[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Garbage collection

Ken Seehart

2/18/2008 9:57:00 PM

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
>
>

6 Answers

aahz

2/19/2008 12:57:00 AM

0

In article <mailman.937.1203371803.9267.python-list@python.org>,
Ken <ken@seehart.com> wrote:
>Simon Pickles wrote:
>>
>> 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.

Assuming we're talking about CPython, objects are deleted immediately
when there are no references to the object. The problem is that it's
not always obvious when the refcount goes to zero.
--
Aahz (aahz@pythoncraft.com) <*> http://www.python...

"All problems in computer science can be solved by another level of
indirection." --Butler Lampson

Jarek Zgoda

2/19/2008 8:51:00 AM

0

Ken napisa3(a):

> 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.

Is that true assumption that __del__ has the same purpose (and same
limitations, i.e. the are not guaranteed to be fired) as Java finalizer
methods?

--
Jarek Zgoda
Skype: jzgoda | GTalk: zgoda@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)

Duncan Booth

2/19/2008 9:15:00 AM

0

Jarek Zgoda <jzgoda@o2.usun.pl> wrote:

> Ken napisa³(a):
>
>> 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.
>
> Is that true assumption that __del__ has the same purpose (and same
> limitations, i.e. the are not guaranteed to be fired) as Java finalizer
> methods?
>

Pretty much. If you have a __del__ method on an object then in the worst
case the only thing that can be guaranteed is that it will be called zero,
one or more than one times. (Admittedly the last of these only happens if
you work at it).

If it is called then is may be either immediately the last reference to the
object is lost, or it may be later when the garbage collector runs (and not
necessarily the first next time the garbage collector runs).

The nasty case to watch for is when __del__ is called while the program is
exiting: any global variables in the module may have already been cleared
so you cannot be sure that you can reference anything other than attributes
on the object being destroyed (and if you call methods on the same or other
objects they may also find they cannot reference all their globals).

Fortunately cases when you actually need to use __del__ are very rare.

Jarek Zgoda

2/19/2008 9:33:00 AM

0

Duncan Booth napisa3(a):

> Pretty much. If you have a __del__ method on an object then in the worst
> case the only thing that can be guaranteed is that it will be called zero,
> one or more than one times. (Admittedly the last of these only happens if
> you work at it).
>
> If it is called then is may be either immediately the last reference to the
> object is lost, or it may be later when the garbage collector runs (and not
> necessarily the first next time the garbage collector runs).

Java finalizers are not called upon VM exit, only when object is swept
by GC (for example when the object is destroyed upon program exit), the
CPython docs read that this is the case for Python too. Is this
behaviour standard for all VM implementations or is
implementation-dependent (CPython, Jython, IronPython)?

--
Jarek Zgoda
Skype: jzgoda | GTalk: zgoda@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)

Duncan Booth

2/19/2008 10:18:00 AM

0

Jarek Zgoda <jzgoda@o2.usun.pl> wrote:

> Duncan Booth napisa³(a):
>
>> Pretty much. If you have a __del__ method on an object then in the
>> worst case the only thing that can be guaranteed is that it will be
>> called zero, one or more than one times. (Admittedly the last of
>> these only happens if you work at it).
>>
>> If it is called then is may be either immediately the last reference
>> to the object is lost, or it may be later when the garbage collector
>> runs (and not necessarily the first next time the garbage collector
>> runs).
>
> Java finalizers are not called upon VM exit, only when object is swept
> by GC (for example when the object is destroyed upon program exit),
> the CPython docs read that this is the case for Python too. Is this
> behaviour standard for all VM implementations or is
> implementation-dependent (CPython, Jython, IronPython)?
>
Yes, CPython does reference counting so it can call __del__ immediately
an object is unreferenced. The GC only comes into play when there is a
reference loop involved. If an object is directly involved in a
reference loop then __del__ is not called for that object, but a loop
could reference another object and its __del__ would be called when the
loop was collected.

Other Python implementations may behave differently: presumably Jython
works as for Java (but I don't know the details of that), and IronPython
uses the CLR which has its own peculiarities: finalizers are all called
on a single thread which is *not* the thread used to construct the
object, so if you use finalizers in a CLR program your program is
necessarily multi-threaded with all that implies. Also it takes at least
two GC cycles to actually release memory on a CLR object with a
finalizer, on the first cycle objects subject to finalization are simply
added to a list (so are again referenceable), on the second cycle if the
finalizer has completed and the object is unreferenced it can be
collected. CLR finalizers also have the interesting quirk that before
the finalizer is called any references the object has to other objects
are cleared: that allows the system to call finalizers in any order.

Otherwise I think the behaviour on exit is pretty standard. If I
remember correctly there is a final garbage collection to give
finalizers a chance to run. Any objects which become newly unreferenced
as a result of that garbage collection will have __del__ called as
usual, but any which merely become unreachable and therefore would be
caught in a subsequent garbage collection won't.

Duncan Booth

2/19/2008 10:39:00 AM

0

Jarek Zgoda <jzgoda@o2.usun.pl> wrote:

> Is that true assumption that __del__ has the same purpose (and same
> limitations, i.e. the are not guaranteed to be fired) as Java finalizer
> methods?

One other point I should have mentioned about __del__: if you are running
under Windows and the user hits Ctrl+Break then unless you handle it Python
will exit without doing any cleanup at all (as opposed to any other method
of exiting such as Ctrl+C). If this matters to you then you can install a
signal handler to catch the Ctrl+Break and exit cleanly.