"Help needed - I don't understand how Python manages memory"

sturlamolden sturlamolden at yahoo.no
Sun Apr 20 16:22:41 CEST 2008

On Apr 20, 2:46 pm, "Hank @ITGroup" <hank.info... at gmail.com> wrote:

> That is my question, after ``del``, sometimes the memory space returns
> back as nothing happened, sometimes not... ...
> What exactly was happening???

Python has a garbage collector. Objects that cannot be reached from
any scope is reclaimed, sooner or later. This includes objects with
reference count of zero, or objects that participate in unreachable
reference cycles. Since Python uses a reference counting scheme, it
does not tend to accumulate so much garbage as Java or .NET. When the
reference count for an object drops to zero, it is immediately freed.

You cannot control when Python's garbage collector frees an object
from memory.

What del does is to delete the object reference from the current
scope. It does not delete the object from memory. That is, the del
statement decrements the reference count by one, and removes the
reference from the current scope. Whether it should removed completely
depends on whether someone else is using it. The object is not
reclaimed unless the reference count has dropped all the way down to
zero. If there still are references to the object other places in your
program, it is not reclaimed upon your call to del.

More information about the Python-list mailing list