473,672 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python memory deallocate

Hi,
I've a big memory problem with my application.

First, an example:

If I write:

a = range(500*1024)

I see that python process allocate approximately 80Mb of memory.
What can i do for DEALLOCATE this memory, or good part of this?

My really problem is that my program work with more photos, which I
open with PIL package.
When I start it, my program allocate approximately 200Mb memory.

If I want abort actual work and restart without restarting python
process, the memory usage will go up approximately 380-400 Mb.

I would like find something that DEallocate the memory not used.

I've tried with gc python module, but don't work fine (it deallocate
approximately 20-30 Mb)
I've tried with Destroy, del command, but the memory don't show down.

Thx

I'm very desperate

May 11 '06 #1
20 9348
Hi,

'del a' should remove 'a', as a reference to the tuple created by the
'range' function.
If that is also the last reference, it can now be garbage-collected.

Of course, the question is if you really need to allocate such a big
amount of memory. If you just need to iterate over some numbers, it's
better to use 'xrange' instead of 'range': 'xrange' will not create the
whole list of numbers in advance, but will create an iterator,
producing the desired numbers one by one. With such large ranges, this
will reduce memory consumption significantly.

Cheers,

--Tim

May 11 '06 #2
O
I see

May 11 '06 #3
ma************* *@gmail.com wrote:
Hi,
I've a big memory problem with my application.

First, an example:

If I write:

a = range(500*1024)

I see that python process allocate approximately 80Mb of memory.
What can i do for DEALLOCATE this memory, or good part of this?

My really problem is that my program work with more photos, which I
open with PIL package.
When I start it, my program allocate approximately 200Mb memory.

If I want abort actual work and restart without restarting python
process, the memory usage will go up approximately 380-400 Mb.

I would like find something that DEallocate the memory not used.

I've tried with gc python module, but don't work fine (it deallocate
approximately 20-30 Mb)
I've tried with Destroy, del command, but the memory don't show down.

Thx

I'm very desperate


No need to. Just because the process seems to be that large doesn't mean
that there is so much memory consumed by python. It's just that modern OSs
will not correctly display the amount of memory really consumed, as they
aren't too eager to remove once allocated virtual memory a process has been
granted. I guess it's an optimization scheme. For example if I do
a = range(500*1024 * 50)

the VmSize of the python process grows to 500MB. If I issue an
del a
it shrinks to 300MB. The reissuing
a = range(500*1024 * 50)
will make it grow - but only to about 400MB. Then I do
del a
a = [chr(i % 255) for i in xrange(500*1024 * 50)]

This time I used strings, to circumvene some internal caching python might
do on numbers. Yet still - the overall VmSize is 400MB

Bottomline: don't get confused by tsakmanagers memory size display. You only
have to be concerned when the consumption steadily grows - which indicates
a memory leak.

Diez

May 11 '06 #4
Well, it's right about range diff xrange, ok, but this was a simple
example...
I repeat that my program don't work with range or xrange...

I MUST find a system which deallocate memory...
Otherwise, my application crashes not hardly it's arrived to
break-point system

May 11 '06 #5
<ma************ **@gmail.com> wrote:
If I write:

a = range(500*1024)

I see that python process allocate approximately 80Mb of memory.
What can i do for DEALLOCATE this memory, or good part of this?
[ ... ]
I've tried with Destroy, del command, but the memory don't show down.


It won't (much). When an object gets garbage collected Python
will keep hold of the memory and reuse it. Note how much memory
your process is using after assigning a above, then:
del a
Of course, you've seen this doesn't release back to the OS all
the memory being that was being used. But now do:
a = range(500*1024)


and you'll see that you're using no more memory than you were
after the first assignment. If your memory usage keeps on growing
then either (a) your program needs that much memory for the data,
and you'll just have to stick more in your box or deal with
swapping if this is causing you a problem or (b) you've got some
stray references left over to objects you think you've deleted.

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
May 11 '06 #6
Ok, this is true.

Well, you consider that my app has a first windows, where I choose, for
example, the application 1.
The application 1 will be started, and it will allocate 200Mb total.
Now I want to abort this operation, and i will return to main initial
window. The memory usage remain to 200Mb, even if I've destroyed the
application 1 object with app1.Destroy() and del app1.
When from initial windows i re-choose application1, the memory usage
don't stay to 200Mb, but will increase to 360-380Mb memory, and I think
this is not good!!!

If I will repeat this procedure for 5-6 times, the application will
crash...

And it's not good!!!

May 11 '06 #7
Am Donnerstag 11 Mai 2006 15:15 schrieb ma************* *@gmail.com:
I MUST find a system which deallocate memory...
Otherwise, my application crashes not hardly it's arrived to
break-point system


As was said before: as long as you keep a reference to an object, the object's
storage _will not be_ reused by Python for any other objects (which is
sensible, or would you like your object to be overwritten by other objects
before you're done with them?). Besides, even if Python did free the memory
that was used, the operating system wouldn't pick it up (in the general case)
anyway (because of fragmentation issues), so Python keeping the memory in an
internal free-list for new objects is a sensible choice the Python developers
took here.

Basically, what I think is happening is that you are loading all images that
you use for your program into memory at once, and these simply will eat up
your memory until your program crashes (because Python can't know you no
longer need them). As you keep a reference to the image (somewhere,
someplace), del(eting) the reference in one scope isn't going to free the
memory for Python (or PIL in this case) to reuse for the next image. An
object is kept alive because of a memory leak inherent to your application.

This is a programming problem, not a Python problem. And, if you don't post
any sources, we won't be able to help you much here, save to tell you to look
closely where you create objects, and where you store them to.

PIL isn't known to have any memory leaks, by the way (AFAICT), just to confirm
what I've written before, but the effbot should be of more help here...

--- Heiko.
May 11 '06 #8
Heiko Wundram wrote:
Am Donnerstag 11 Mai 2006 15:15 schrieb ma************* *@gmail.com:
I MUST find a system which deallocate memory...
Otherwise, my application crashes not hardly it's arrived to
break-point system


As was said before: as long as you keep a reference to an object, the object's
storage _will not be_ reused by Python for any other objects (which is
sensible, or would you like your object to be overwritten by other objects
before you're done with them?). Besides, even if Python did free the memory
that was used, the operating system wouldn't pick it up (in the general case)
anyway (because of fragmentation issues), so Python keeping the memory in an
internal free-list for new objects is a sensible choice the Python developers
took here.


BTW python 2.5 now returns free memory to OS, but if a program keeps
allocating more memory with each new iteration in python 2.4, it will
not help.

May 11 '06 #9
ma************* *@gmail.com wrote:
Ok, this is true.

Well, you consider that my app has a first windows, where I choose, for
example, the application 1.
The application 1 will be started, and it will allocate 200Mb total.
Now I want to abort this operation, and i will return to main initial
window. The memory usage remain to 200Mb, even if I've destroyed the
application 1 object with app1.Destroy() and del app1.
When from initial windows i re-choose application1, the memory usage
don't stay to 200Mb, but will increase to 360-380Mb memory, and I think
this is not good!!!

If I will repeat this procedure for 5-6 times, the application will
crash...


Either there are references somewhere keeping some objects alive, or
you're facing a known problem with pymalloc() [1]. In the second case,
the good news is that this problem seems to be solved in Python 2.5 [2].
The bad news is that Python 2.5 is still alpha...

Just a question: do you really need your app to be monolithic ? If
you're application is really composed of many applications (which is
what I understand from your example), you should probably have many
distinct applications, one of them being in charge of running the
others. This way, 'aborting' an app would kill the corresponding
process, and free memory.
[1] or it's yet another problem, of course - CS wouldn't be that fun if
it was so simple :(

[2] http://docs.python.org/dev/whatsnew/section-other.html

My 2 cents

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 11 '06 #10

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

Similar topics

3
5104
by: Marcelo A. Camelo | last post by:
Hi! I will be presenting Python to an audience of game developers, mostly C/C++ programmers. In my presentation I will talk about using python and C/C++ extension instead of pure C/C++ to write commercial games and content creation tools. In a number of times I have presented these ideas to this same kind of audience and they were received with cold skepticism, despite the fact that some, including me, are already doing this with...
4
1730
by: Roy Smith | last post by:
I understand that psyco significantly increases memory use. Is that for code or data? More specifically, if I've got a memory intensive application (it might use 100's of Mbytes of data), should I expect memory use to go up significantly under psyco? Also, for that memory intensive application, how should I expect Python memory use to compare with C++? I'm really only interested in data; the memory needed to store the code is almost...
0
1720
by: Stephen Kellett | last post by:
Announcing Software Tools for Python We are pleased to inform you that we have completed the port and beta test of our Memory Analysis software tool to support Python. The software tools run on the Windows NT/2000/XP (and above) platforms. Python Memory Validator (a memory leak detection tool) http://www.softwareverify.com/python/memory/index.html The website provides 30 day evaluation versions of the software as well
13
4479
by: placid | last post by:
Hi All, Just wondering when i run the following code; for i in range(1000000): print i the memory usage of Python spikes and when the range(..) block finishes execution the memory usage does not drop down. Is there a way of freeing this memory that range(..) allocated?
17
8467
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML file under linux python 2.4 (same problem with windows 2.5, tried with the first example) : #Python interpreter memory usage : 1.1 Mb private, 1.4 Mb shared #Using http://www.pixelbeat.org/scripts/ps_mem.py to get memory information
0
3078
by: greg.novak | last post by:
I am using Python to process particle data from a physics simulation. There are about 15 MB of data associated with each simulation, but there are many simulations. I read the data from each simulation into Numpy arrays and do a simple calculation on them that involves a few eigenvalues of small matricies and quite a number of temporary arrays. I had assumed that that generating lots of temporary arrays would make my program run slowly,...
0
8423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8948
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8852
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8701
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7482
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6261
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4251
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2847
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 we have to send another system
3
1843
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.