Hooking into Python's memory management

sturlamolden sturla at molden.no
Wed May 4 13:11:33 EDT 2011


On May 4, 6:51 pm, Daniel Neilson <ddneil... at gmail.com> wrote:

>   In either case, if such a module is possible, any pointers you could
> provide regarding how to implement such a module would be appreciated.


The gc module will hook into the garbage collector.

The del statement will remove an object from the current scope.
(Delete the variable name and decrement the reference count.)

Python (CPython that is) is reference counted. When the refcount drops
to zero, the object is immediately garbage collected. Python is not
like Java, where this happen in bouts. The __del__ method is executed
deterministically, it's not like a finalizer in Java or C#.

Only dead objects involved in reference circles may linger until they
are spotted by the GC. And they may not have a __del__ method, or else
the GC will ignore them.

In fact, if you don't create circular references, the GC can safely be
turned off.

If you want volatile references, Python allows weak references as
well.


Sturla







More information about the Python-list mailing list