[Tutor] general question about memory leaks in python

Abel Daniel abli@freemail.hu
Tue Mar 25 16:55:45 2003


Isaac Hall  wrote:
[ ...snipped description of a memory-eating program... ]

Python uses garbage-collecting. Anything which isn't reachable from you
program (objects for which you don"t have a reference anywhere, or where
the references are only between a set of objects, none of which you can
reach, cyclic garbage) gets garbage-collected.
This means that the only way of having memory-leaks is having references
to objects you no longer need.

Some remarks:
garbage gets collected as soon as you drop the last reference to it in
the C implementation. In Jython (python implemented in Java) Java memory
management is used, so garbage may be collected later. Also, for collecting
cyclic garbage, a special part of python awakens at a regular interval,
looks around for cyclic garbage then breaks those cycles. (So collecting
cyclic garbage isn't instanteneus.)
You can have old-fashioned memory leaks in for example C extensions.
There is nothing python could do about it.
To tinkter around with the memory management, check out the gc module.
For example, gc.get_objects() is the place I would start to find
unneeded references. (Provided you don't use non-python extensions (what do
you use for the graphics?), in which case check whether they know of any
memory leaks.)

Disclaimer: all this gleaned for lurking in comp.lang.python for some
months. I never had to debug anything memory-related with python, and
all this might be wrong.
http://python.org/doc/current/ref/objects.html says:

Objects are never explicitly destroyed; however, when they become
unreachable they may be garbage-collected. An implementation is allowed
to postpone garbage collection or omit it altogether -- it is a matter
of implementation quality how garbage collection is implemented, as long
as no objects are collected that are still reachable. (Implementation
note: the current implementation uses a reference-counting scheme with
(optional) delayed detection of cyclicly linked garbage, which collects
most objects as soon as they become unreachable, but is not guaranteed
to collect garbage containing circular references. See the Python
Library Reference[link to gc module] for information on controlling
the collection of cyclic garbage.)

So I can't be that far off.. :)

Abel Daniel
ps. I saw code which explicitly broke cycles to avoid cyclicc garbage,
but that might have been needed before cyclic gc was in the core python.
Dont know about that.
If all else fails and you have a short snippet of code which shows the
problem, or a specific problem, try comp.lang.python (which is also
available as a mailing list.)