Garbage collection

Ken ken at seehart.com
Mon Feb 18 16:56:34 EST 2008


Simon Pickles wrote:
> Hi,
>
> I'm building a server with python, but coming from a c++ background, 
> garbage collection seems strange.
>
> For instance, I have a manager looking after many objects in a dict. 
> When those objects are no longer needed, I use del manager[objectid], 
> hoping to force the garbage collector to perform the delete.
>
> However, this doesn't trigger my overloaded __del__ destructor. Can I 
> simply rely on the python garbage collector to take it from here?
>   
Objects are deleted at some undefined time after there are no references 
to the object.

You will need to change your thinking about how destructors work.  It is 
very different from C++.

The good news is that you almost never have to do anything to clean up.  
My guess is that you might not even need to overload __del__ at all.  
People from a C++ background often mistakenly think that they have to 
write destructors when in fact they do not.  What is your __del__ method 
doing?
> Is there a way to find how many references exist for an object?
>   
yes:

  from sys import getrefcount
  print getrefcount(x)


> Thanks
>
> Simon
>
>   




More information about the Python-list mailing list