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

Discussion in 'Python' started by Jane Austine, Jul 2, 2003.

  1. Jane Austine

    Jane Austine Guest

    "Aahz" <> wrote in message news:bdtlji$pmh$...
    > In article <>,
    > Jane Austine <> 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 () <*> 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?
    Jane Austine, Jul 2, 2003
    #1
    1. Advertising

  2. 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 && && 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
    Erik Max Francis, Jul 2, 2003
    #2
    1. Advertising

  3. 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
    ,-= U
    1 1
    Steven Taschuk, Jul 2, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Austine
    Replies:
    1
    Views:
    468
    Erik Max Francis
    Jul 2, 2003
  2. Christopher J. Bottaro

    super not working in __del__ ?

    Christopher J. Bottaro, Feb 15, 2005, in forum: Python
    Replies:
    0
    Views:
    317
    Christopher J. Bottaro
    Feb 15, 2005
  3. Fredrik Lundh

    Re: super not working in __del__ ?

    Fredrik Lundh, Feb 16, 2005, in forum: Python
    Replies:
    5
    Views:
    584
    Duncan Booth
    Feb 16, 2005
  4. Christopher J. Bottaro

    Re: super not working in __del__ ?

    Christopher J. Bottaro, Feb 16, 2005, in forum: Python
    Replies:
    5
    Views:
    769
    Jeff Shannon
    Feb 18, 2005
  5. AMLiapunov

    Scalars Leaked and Segmentation Fault

    AMLiapunov, Feb 21, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    110
    zentara
    Feb 23, 2006
Loading...

Share This Page