[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

Simon Pickles

2/19/2008 8:39:00 AM

Ken wrote:
> What is your __del__ method doing?
>
Actually, nothing but printing a message when the object is deleted,
just morbid curiosity.

I've yet to see one of the destructor messages, tho....

>
> from sys import getrefcount
> print getrefcount(x)
>
>
Perfect, thanks

Simon
5 Answers

Hrvoje Niksic

2/19/2008 9:48:00 AM

0

Simon Pickles <sipickles@googlemail.com> writes:

> Ken wrote:
>> What is your __del__ method doing?
>>
> Actually, nothing but printing a message when the object is deleted,
> just morbid curiosity.
>
> I've yet to see one of the destructor messages, tho....

Do your objects participate in reference cycles? In that case they
are deallocated by the cycle collector, and the cycle collector
doesn't invoke __del__.

>>> class X(object):
.... def __del__(self): print "gone"
....
>>> a = X()
>>> a = 1
gone
>>> b = X()
>>> b.someslot = b
>>> b = 1
>>> import gc
>>> gc.collect()
0
>>>

Nick Craig-Wood

2/19/2008 12:30:00 PM

0

Hrvoje Niksic <hniksic@xemacs.org> wrote:
> Simon Pickles <sipickles@googlemail.com> writes:
>
> > Ken wrote:
> >> What is your __del__ method doing?
> >>
> > Actually, nothing but printing a message when the object is deleted,
> > just morbid curiosity.
> >
> > I've yet to see one of the destructor messages, tho....
>
> Do your objects participate in reference cycles? In that case they
> are deallocated by the cycle collector, and the cycle collector
> doesn't invoke __del__.
>
> >>> class X(object):
> ... def __del__(self): print "gone"
> ...
> >>> a = X()
> >>> a = 1
> gone
> >>> b = X()
> >>> b.someslot = b
> >>> b = 1
> >>> import gc
> >>> gc.collect()
> 0
> >>>

If you want to avoid this particular problem, use a weakref.

>>> c = X()
>>> from weakref import proxy
>>> c.weak_reference = proxy(c)
>>> c.weak_reference.__del__
<bound method X.__del__ of <__main__.X object at 0xb7d1e56c>>
>>> c = 1
>>> gc.collect()
gone
0
>>>

Or perhaps slightly more realistically, here is an example of using a
WeakKeyDictionary instead of __del__ methods for keeping an accurate
track of all classes of a given type.

from weakref import WeakKeyDictionary

class Y(object):
_registry = WeakKeyDictionary()
def __init__(self):
self._registry[self] = True
@classmethod
def list(cls):
return cls._registry.keys()

a = Y()
b = Y()
c = Y()
Y.list()
a = 1
c = 1
Y.list()

Which produces the output

[<__main__.Y object at 0xb7d9fc8c>, <__main__.Y object at 0xb7d9fcac>, <__main__.Y object at 0xb7d9fc2c>]
[<__main__.Y object at 0xb7d9fc8c>]

(It behaves slightly differently in the interactive interpreter for
reasons I don't understand - so save it to a file and try it!)

In fact I find most of the times I wanted __del__ can be fixed by
using a weakref.WeakValueDictionary or weakref.WeakKeyDictionary for a
much better result.

--
Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-woo...

Duncan Booth

2/19/2008 1:26:00 PM

0

Nick Craig-Wood <nick@craig-wood.com> wrote:

> [<__main__.Y object at 0xb7d9fc8c>, <__main__.Y object at 0xb7d9fcac>,
> <__main__.Y object at 0xb7d9fc2c>] [<__main__.Y object at 0xb7d9fc8c>]
>
> (It behaves slightly differently in the interactive interpreter for
> reasons I don't understand - so save it to a file and try it!)

Any expression in the interactive interpreter is implicitly assigned to
the variable '_', so after your first call to Y.list() you've saved
references to the complete list in _. Assignments aren't expressions so
after assigning to a and c you haven't changed _. If you throw in
another unrelated expression you'll be fine:

>>> a = Y()
>>> b = Y()
>>> c = Y()
>>> Y.list()
[<__main__.Y object at 0x0117F230>, <__main__.Y object at 0x0117F2B0>,
<__main__.Y object at 0x0117F210>, <__main__.Y object at 0x0117F670>,
<__main__.Y object at 0x0117F690>, <__main__.Y object at 0x0117F6B0>,
<__main__.Y object at 0x0117F310>]
>>> a = 1
>>> c = 1
>>> c
1
>>> Y.list()
[<__main__.Y object at 0x0117F6B0>]

> In fact I find most of the times I wanted __del__ can be fixed by
> using a weakref.WeakValueDictionary or weakref.WeakKeyDictionary for a
> much better result.

The WeakValueDictionary is especially good when you want a Python
wrapper round some external non-python thing, just use the address of
the external thing as the key for the dictionary and you can avoid
having duplicate Python objects.

The other option for classes involved in a cycle is to move the __del__
(and anything it needs) down to another class which isn't part of the
cycle, so the original example becomes:

>>> class Monitor(object):
def __del__(self): print "gone"


>>> class X(object):
def __init__(self):
self._mon = Monitor()


>>> a = X()
>>> a = 1
gone
>>> b = X()
>>> b.someslot = b
>>> b = 1
>>> import gc
>>> gc.collect()
gone
8
>>>

Unhappy Observer

7/26/2010 1:45:00 AM

0


"Michael Ragland" <michaeleugeneragland@gmail.com> wrote in message
news:2f1748ef-3781-415f-8fb7-96a27b4666ac@c10g2000yqi.googlegroups.com...
On Jul 24, 9:07 am, Ben Cramer <bencram...@gmail.com> wrote:
> On Jul 24, 4:34 pm, Michael Ragland <michaeleugeneragl...@gmail.com>
> wrote:
>
>
>
> > On Jul 23, 8:47 pm, Ben Cramer <bencram...@gmail.com> wrote:
>
> > >Survivoris more than a number
> > > Dario Gabbai, who was interned at Auschwitz, shares painful memories
> > > with Chaffey class.
> > > Jeff Benson
> > > Claremont-Upland Voice
> > > April 9,
> > > 2004http://www.latimes.com/news/local/clv/la-clv-genocide09apr0......
> > > "182568."
> > > The teary-eyed man recited the number quickly and without looking,
> > > dejectedly revealing to a startled Chaffey College class last Friday
> > > why it's tattooed on his left arm.
> > > "I tell my story because there's very few left anymore who can tell
> > > it," he said.
> > > An Auschwitzsurvivor, Dario Gabbai doesn't feel like just a number
> > > anymore. But the fact he still bears one reminds him of other numbers
> > > -- the estimated 6 million people slaughtered under the Nazi regime;
> > > the 54,000 Jews herded like cattle from his hometown of Salonika,
> > > Greece, to the Auschwitz labor and death camp in 1944, and the 12,000
> > > people killed there every day, including his own mother and sister.
> > > And at 82, the Italian-Greek Jew still feels he had a hand in
> > > unspeakable evils.
> > > With the German Security Service aiming a gun at his back at all
> > > times, Gabbai was forced to assist the Nazis in their extermination of
> > > Jews at the nearby Auschwitz-Birkenau labor and death camp.
> > > "There was no choice," he said. "If you didn't do it, the guy behind
> > > you was going to kill you. That's it."
> > > As sonderkommandos in Birkenau duringWorld War II, Gabbai and his two
> > > cousins ushered Hungarian Jews into gas chambers disguised as
> > > "showers," removed the bodies after the Zyklon-B poisoned gas aired
> > > out and discarded them into crematoriums in order to dispose of the
> > > evidence of Nazi crimes.
> > > He said, at times, close to 2,000 people packed into gas chambers with
> > > capacities of 500. Gabbai tried to make the best of an impossible
> > > situation, though, going as far as telling those who were about to die
> > > exactly where they should move inside the chamber so they could die
> > > quickly with the least amount of suffering.
> > > [...]
> > > "Did you ever wish you were the one being killed?" one student asked
> > > him.
> > > "Yeah," he said immediately. "Many times."
> > > [...]
>
> > The link doesn't work. The story sounds believable. Why are you
> > posting it? Because you find it too outlandish to be factual? There
> > are many witnesses/survivors of the Holocaust who have wrote about
> > their nightmarish ordeal. By far most of them are not frauds.
>
> > Michael Ragland- Hide quoted text -
>
> > - Show quoted text -
>
> The link does work, silly little man.
>
> Did you ever receive an education beyond grade school? Judging by
> your spelling and grammar, I suspect not.
>
> You are one dim-witted twerp.

>No, on my computer all I get is a blank page. The link does not work.

Works on mine OK ragland.



Jason P

7/27/2010 4:07:00 AM

0


"Michael Ragland" <michaeleugeneragland@gmail.com> wrote in message
news:2f1748ef-3781-415f-8fb7-96a27b4666ac@c10g2000yqi.googlegroups.com...
On Jul 24, 9:07 am, Ben Cramer <bencram...@gmail.com> wrote:
> On Jul 24, 4:34 pm, Michael Ragland <michaeleugeneragl...@gmail.com>
> wrote:
>
>
>
> > On Jul 23, 8:47 pm, Ben Cramer <bencram...@gmail.com> wrote:
>
> > >Survivoris more than a number
> > > Dario Gabbai, who was interned at Auschwitz, shares painful memories
> > > with Chaffey class.
> > > Jeff Benson
> > > Claremont-Upland Voice
> > > April 9,
> > > 2004http://www.latimes.com/news/local/clv/la-clv-genocide09apr0......
> > > "182568."
> > > The teary-eyed man recited the number quickly and without looking,
> > > dejectedly revealing to a startled Chaffey College class last Friday
> > > why it's tattooed on his left arm.
> > > "I tell my story because there's very few left anymore who can tell
> > > it," he said.
> > > An Auschwitzsurvivor, Dario Gabbai doesn't feel like just a number
> > > anymore. But the fact he still bears one reminds him of other numbers
> > > -- the estimated 6 million people slaughtered under the Nazi regime;
> > > the 54,000 Jews herded like cattle from his hometown of Salonika,
> > > Greece, to the Auschwitz labor and death camp in 1944, and the 12,000
> > > people killed there every day, including his own mother and sister.
> > > And at 82, the Italian-Greek Jew still feels he had a hand in
> > > unspeakable evils.
> > > With the German Security Service aiming a gun at his back at all
> > > times, Gabbai was forced to assist the Nazis in their extermination of
> > > Jews at the nearby Auschwitz-Birkenau labor and death camp.
> > > "There was no choice," he said. "If you didn't do it, the guy behind
> > > you was going to kill you. That's it."
> > > As sonderkommandos in Birkenau duringWorld War II, Gabbai and his two
> > > cousins ushered Hungarian Jews into gas chambers disguised as
> > > "showers," removed the bodies after the Zyklon-B poisoned gas aired
> > > out and discarded them into crematoriums in order to dispose of the
> > > evidence of Nazi crimes.
> > > He said, at times, close to 2,000 people packed into gas chambers with
> > > capacities of 500. Gabbai tried to make the best of an impossible
> > > situation, though, going as far as telling those who were about to die
> > > exactly where they should move inside the chamber so they could die
> > > quickly with the least amount of suffering.
> > > [...]
> > > "Did you ever wish you were the one being killed?" one student asked
> > > him.
> > > "Yeah," he said immediately. "Many times."
> > > [...]
>
> > The link doesn't work. The story sounds believable. Why are you
> > posting it? Because you find it too outlandish to be factual? There
> > are many witnesses/survivors of the Holocaust who have wrote about
> > their nightmarish ordeal. By far most of them are not frauds.
>
> > Michael Ragland- Hide quoted text -
>
> > - Show quoted text -
>
> The link does work, silly little man.
>
> Did you ever receive an education beyond grade school? Judging by
> your spelling and grammar, I suspect not.
>
> You are one dim-witted twerp.
--------
>No, on my computer all I get is a blank page. The link does not work.
>I see all you can do is hurl petty insults. Who is the silly little
>man?

>Michael Ragland

If you couldn't get that link to work you have something buggy going on.
Re-load your OS.