Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: __del__ not working with cyclic reference? (and memory-leaked?) (http://www.velocityreviews.com/forums/t319110-re-__del__-not-working-with-cyclic-reference-and-memory-leaked.html)

Jane Austine 07-02-2003 05:46 AM

Re: __del__ not working with cyclic reference? (and memory-leaked?)
 
"Aahz" <aahz@pythoncraft.com> wrote in message news:bdtlji$pmh$1@panix2.panix.com...
> In article <ba1e306f.0307011837.269e3d2a@posting.google.com >,
> Jane Austine <janeaustine50@hotmail.com> wrote:
> >
> >I have some code using singleton pattern. The singleton instance
> >is shared as a class variable. The problem is that the singleton
> >instance is not cleared automatically.
> >
> >Following is a simplified version that shows the problem:
> >
> >--------
> >#foobar.py
> >class FooBar:
> > def __del__(self):
> > print "FooBar removed"
> >
> >FooBar.foobar=FooBar()
> >--------
> >
> >Why is this so? Due to the cyclic reference? Isn't python's gc
> >supposed to treat it? What singleton idiom is recommended
> >otherwise?

>
> There are two issues that you'll run into with this: gc doesn't work on
> classes that define __del__(), and prior to Python 2.3, gc doesn't run
> on exit.
> --
> Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
>


I read the gc module reference thoroughly and
thanks to your short comment, I could understand it.
(I should not use __del__ for singleton patterns, but
any other alternatives?)

But does the fact that __del__ is not called for some
objects mean they are not freed from memory and therefore
memory-leaked?

Erik Max Francis 07-02-2003 05:56 AM

Re: __del__ not working with cyclic reference? (and memory-leaked?)
 
Jane Austine wrote:

> I read the gc module reference thoroughly and
> thanks to your short comment, I could understand it.
> (I should not use __del__ for singleton patterns, but
> any other alternatives?)


I'm not sure why you'd want to use __del__ for a singleton pattern in
the first place, since singleton patterns are supposed to cushion the
creation and destruction of the singleton object away from the interface
user. What are you trying to do with singleton patterns as it relates
to __del__?

> But does the fact that __del__ is not called for some
> objects mean they are not freed from memory and therefore
> memory-leaked?


No, most operating systems guarantee that when a process exits any
memory it allocated through normal means will be freed whether or not
the application explicitly does it. So just as with C, C++, or Java,
Python ensures that when the program exits you won't have leaked memory
away from your operating system.

However, Python doesn't make any guarantees about how timely __del__
will be called, or indeed even if it will be (in the case of circular
references). If you're finding yourself needing to rely on using
__del__ to get rid of important objects (such as those that are attached
to significant resources), a far better solution would be to use try:
.... finally: clauses surrounding the usage of such objects:

resource = Resource()
try:
... use resource ...
finally:
resource.close()

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Men and women, women and men. It will never work.
\__/ Erica Jong

Steven Taschuk 07-02-2003 02:36 PM

Re: __del__ not working with cyclic reference? (and memory-leaked?)
 
Quoth Jane Austine:
[...]
> (I should not use __del__ for singleton patterns, but
> any other alternatives?)


You could register an atexit procedure; see the atexit module in
the standard library.

You *can* use __del__ for a singleton as long as there is no
cyclic reference, or if the interpreter deals with the cycle
anyway. Under 2.2.2 and 2.3b1 this seems to work:

# foo.py
class MySingleton(object):
def __del__(self):
print 'foo'
mysingleton = MySingleton()

The difference is that here the instance is referred to by the
module dict rather than the MySingleton class dict. (I think the
reason this makes a difference is that interpreter shutdown
includes a zapping of module dicts, breaking this cycle:

>>> import foo
>>> d = foo.__dict__
>>> d['mysingleton'].__class__.__dict__['__del__'].func_globals is d

True

Note that if your __del__ refers to global variables, they might
have been zapped by the time it gets called.)

--
Steven Taschuk w_w
staschuk@telusplanet.net ,-= U
1 1



All times are GMT. The time now is 04:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.