Python memory deallocate

Diez B. Roggisch deets at nospam.web.de
Thu May 11 14:28:55 CEST 2006


mariano.difelice at 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




More information about the Python-list mailing list