By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,417 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,417 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.