Garbage collection on strike?

Aahz Maruch aahz at panix.com
Thu May 10 14:40:46 EDT 2001


In article <3AFA6171.4020805 at herts.ac.uk>,
Mark blobby Robinson  <m.1.robinson at herts.ac.uk> wrote:
>
>I wonder if anyone can throw some enlightenment my way, cos I obviously 
>need some. I am currently working on a program tackling a huge 
>combinatorial problem and the results held in memory are getting pretty 
>big. My solution was to dump the data to file when the list I was 
>generating got to a length of a million, and then explicitly delete the 
>data structure and start over, but the memory doesn't seem to get 
>refreshed. In fact sometimes when the whole program has executed the 
>memory still doesn't seem to have been cleaned up and I am having to at 
>times do a hard reboot of the machine. I am running this on a 700Mhz 
>computer with 730MB RAM running Red hat 7.0. Any suggestions as to the 
>error of my ways?

Which version of Python are you using?  (Not that it really matters for
the two points I'm going to make.)

* You will rarely see memory released from a process until it exits.
However, if you delete all references to a Python object, that memory
should be reclaimed and reusable by other objects.  On the gripping
hand, if you have a particularly malformed use of memory, with a weird
mixture of very large objects (large strings or buffers, basically),
it's theoretically possible to fragment memory in a way that causes
memory usage to keep going up.

* Much more likely, though, is that you're simply not deleting object
references.  Garbage collection in Python only works on circular
references, where two or more objects refer to each other, but none has
any external references pointing in.  If you've got a dict, for example,
that has a reference to the objects that aren't disappearing, your
memory usage will keep going up as you allocate new objects.
-- 
                      --- Aahz  <*>  (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het Pythonista   http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"Everyone is entitled to an *informed* opinion."  --Harlan Ellison



More information about the Python-list mailing list