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

Roy Smith roy at panix.com
Sat Mar 9 22:44:03 CET 2013


In article <khg6f3$cfr$1 at reader2.panix.com>,
 Grant Edwards <invalid at invalid.invalid> wrote:

> What I should have said was that there's no way to return to the OS
> memory obtained via calls to malloc() et al.

That's true (for certain values of "et al").

> and those are the calls that "good" C programmers (like the
> maintainers of CPython) use.

Well, there is mmap, which is exposed via the Python mmap module.  
Python doesn't have anything like C++'s "placement new", so there's no 
way to use that memory to hold generic Python objects, but you can 
certainly use mmap to allocate a large chunk of memory, use it, and then 
give it back to the OS.  For example:

#!/usr/bin/env python                                                                                                                                               

import mmap
import time

time.sleep(5)

f = open('my-500-mbyte-text-file')
data = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)

count = 0
while 1:
    line = data.readline()
    if not line:
        break
    count += 1

print count
time.sleep(5)
data.close()
time.sleep(5)

When I run that and watch the process size (like I did with the previous 
example), you can see the process grow when mmap() is called, and shrink 
again when the segment is closed.

I have to admit, in all the years I've been using Python, this is the 
first time I've ever used the mmap module.  Even for long running 
processes, the automatic memory management that Python provides out of 
the box has always been good enough for me.  But, it's nice to know mmap 
is there if I need to do something unusual.



More information about the Python-list mailing list