Set x to to None and del x doesn't release memory in python 2.7.1 (HPUX 11.23, ia64)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Mar 7 00:34:42 CET 2013


On Wed, 06 Mar 2013 10:11:12 +0000, Wong Wah Meng-R32813 wrote:

> Hello there,
> 
> I am using python 2.7.1 built on HP-11.23 a Itanium 64 bit box.
> 
> I discovered following behavior whereby the python process doesn't seem
> to release memory utilized even after a variable is set to None, and
> "deleted". I use glance tool to monitor the memory utilized by this
> process. Obviously after the for loop is executed, the memory used by
> this process has hiked to a few MB. However, after "del" is executed to
> both I and str variables, the memory of that process still stays at
> where it was.
> 
> Any idea why?

Python does not guarantee to return memory to the operating system. 
Whether it does or not depends on the OS, but as a general rule, you 
should expect that it will not.


>>>> for i in range(100000L):
> ...     str=str+"%s"%(i,)


You should never build large strings in that way. It risks being 
horribly, horribly slow on some combinations of OS, Python implementation 
and version.

Instead, you should do this:

items = ["%s" % i for i in range(100000)]
s = ''.join(items)


-- 
Steven



More information about the Python-list mailing list