python 1.5.2 memory leak?

Skip Montanaro skip at
Sat Nov 18 07:46:27 CET 2000

    Taz> i'm running python code that "runs forver" like daemons. i see that
    Taz> the memory allocated to it keeps growing using ktop....

    Taz> i have taken care to close all objects like file.close() ...
    Taz> socket.close() -- using the python essential reference book - so i
    Taz> don't think i;ve lost anything?

    Taz> can i force garbage collection?

    Taz> i;'m using python 1.5.2 on redhat 6.2 and mandrake 7.1


All versions of Python before 2.0 use simple reference counting memory
management.  When an object's reference count drops to zero, its storage is
reclaimed.  If your memory use is growing, I suspect your data forms cycles
that can't be reclaimed.  Here's a quick example:

    a = []
    del a

After the first statement is executed, an empty list object exists with a
reference count of 1 (the reference from the variable a).  After the second
statement, the reference count has been upped to 2 (the list references
itself).  When the third statement executes, the reference count drops to 1
again because we deleted the reference from a.  The object's storage can't
be reclaimed because its reference count has not dropped to 0.

You can, of course, have more indirection than the above example.

There are a few things you can try:

    * meticulously hand check your code looking for cycles - here are for
      some common sources:
      - lists or dictionaries that reference themselves
      - classes that maintain lists of their instances
      - objects that reference themselves through an attribute

    * upgrade to 2.0 which adds a cyclic garbage collector to the existing
      reference counting scheme

    * grab and use Tim Peters' Cyclops script - I'd search Google for
      "" instead of relying on to have it - also, see the
      egroups reference below

    * browse the Python FAQ - questions 4.17 and 6.14 look pertinent

    * search the mailing list archives at

      looking for discussions of reference counting, cycles and garbage

(P.S. to Ben Ocean - see how much help you can get from this list if you ask
nicely and provide some actual information about your problem?)

Skip Montanaro (skip at
Support the Affiliates Program:

More information about the Python-list mailing list