Why does this leak memory?

Ian Kelly ian.g.kelly at gmail.com
Fri Jun 8 19:04:25 CEST 2012


On Fri, Jun 8, 2012 at 10:02 AM, Steve <no at spam.com> wrote:
> Well, I guess I was confused by the terminology. I thought there were leaked
> objects _after_ a garbage collection had been run (as it said "collecting
> generation 2").

That means that it's going to check all objects.  The garbage
collector divides the objects up into "generations", based on how many
collection attempts they've so far survived (due to not being
unreachable).  For efficiency, higher generation objects are checked
less frequently than lower generation objects, and generation 2 is the
highest.

> Also, "unreachable" actually appears to mean "unreferenced".

Not exactly.  CPython uses reference counting as well as periodic
garbage collection, so an unreferenced object is deallocated
immediately.  "Unreachable" means that the object is referenced but
cannot be reached via direct or indirect reference from any stack
frame -- i.e., the object is only referenced in unreachable reference
cycles.


More information about the Python-list mailing list