473,379 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 3361
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
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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.