473,486 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: "Help needed - I don't understand how Python manages memory"

Steve Holden wrote:
>
You are suffering from a pathological condition yourself: the desire
to optimize performance in an area where you do not have any problems.
I would suggest you just enjoy using Python and then start to ask
these questions again when you have a real issue that's stopping you
from getting real work done.

regards
Steve
Hi, Steve,
This not simply a pathological condition. My people are keeping trying
many ways to have job done, and the memory problem became the focus we
are paying attention on at this moment.
Could you please give us some clear clues to obviously call python to
free memory. We want to control its gc operation handily as we were
using J**A.
Jun 27 '08 #1
3 3383
On Apr 20, 9:09 pm, "Hank @ITGroup" <hank.info...@gmail.comwrote:
Could you please give us some clear clues to obviously call python to
free memory. We want to control its gc operation handily as we were
using J**A.
If you want to get rid of a Python object, the only way to do that is
to get rid of every reference to the object. This is no different from
Java.

If you just want to deallocate and allocate memory to store text,
Python lets you do that the same way as C:
from __future__ import with_statement
import os
from ctypes import c_char, c_char_p, c_long, cdll
from threading import Lock

_libc = cdll.msvcr71 if os.name == 'nt' else cdll.libc
_lock = Lock()

def string_heapalloc(n):
''' allocate a mutable string using malloc '''
with _lock:
malloc = _libc.malloc
malloc.argtypes = [c_long]
malloc.restype = c_char * n
memset = _libc.memset
memset.restype = None
memset.argtypes = [c_char * n, c_char, c_long]
tmp = malloc(n)
memset(tmp,'0',n)
return tmp

def string_heapfree(s):
''' free an allocated string '''
with _lock:
free = _libc.free
free.restype = None
free.argtypes = [c_char_p]
ptr_first_char = c_char_p( s[0] )
free(ptr_first_char)
if __name__ == '__main__':
s = string_heapalloc(1000)
s[:26] = 'abcdefghijklmnopqrstuvwxyz'
print s[:]
string_heapfree(s)

Jun 27 '08 #2
En Sun, 20 Apr 2008 17:19:43 -0300, sturlamolden <st**********@yahoo.noescribió:
On Apr 20, 9:09 pm, "Hank @ITGroup" <hank.info...@gmail.comwrote:
>Could you please give us some clear clues to obviously call python to
free memory. We want to control its gc operation handily as we were
using J**A.

If you want to get rid of a Python object, the only way to do that is
to get rid of every reference to the object. This is no different from
Java.

If you just want to deallocate and allocate memory to store text,
Python lets you do that the same way as C:
I'm not sure if this will help the OP at all - going into a world of dangling pointers, keeping track of ownership, releasing memory by hand... One of the good things of Python is automatic memory management. Ensuring that all references to an object are released (the standard Python way) is FAR easier than doing all that by hand.

--
Gabriel Genellina

Jun 27 '08 #3
On Apr 21, 4:09 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
I'm not sure if this will help the OP at all - going into a world of dangling pointers, keeping track of ownership, releasing memory by hand... One of the good things of Python is automatic memory management. Ensuring that all references to an object are released (the standard Python way) is FAR easier than doing all that by hand.
The owner was complaining he could not manually release memory using
del, as if it was Python's equivalent of a C++ delete[] operator. I
showed him how it could be done. I did not say manual memory
management is a good idea.


Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2172
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
9
5787
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
6
1984
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to...
17
2305
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
5
17244
by: Jesee | last post by:
I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework programming",i came across "Exception handing". Page 405 says "If the stack overflow occurs within the CLR itself,your...
4
6921
by: lawrence k | last post by:
I've a jpeg image that is 514k, which doesn't strike me as very large. Yet I'm running out of error when I try to resize it: Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to...
13
3126
by: Vincent Delporte | last post by:
Hi I'm a Python newbie, and would like to rewrite this Perl scrip to be run with the Asterisk PBX: http://www.voip-info.org/wiki/view/Asterisk+NetCID Anyone knows if those lines are...
3
1465
by: Aries Sun | last post by:
I am reading the book "Effective C++" Item 25 mentioned the following code: // a first cut at a class yielding NULL pointer objects class NullClass { public: template<class T ...
5
1869
by: Tony Toews [MVP] | last post by:
I'm very upset with Google's policy of indexing and allowing Google advertising on forum web sites which are "slurping" content from Usenet and Microsoft newsgroup servers. ...
0
150
by: Steve Holden | last post by:
Hank @ITGroup wrote: It doesn't really make that much sense to watch memory usage as you have been doing. Your first test case appears to trigger a specific pathology, where the memory allocator...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.