[IPython-user] question about garbage collection

killian koepsell koepsell@gmail....
Sun Jul 13 14:28:13 CDT 2008


Hi Fernando,

On Sun, Jul 13, 2008 at 12:48 AM, Fernando Perez <fperez.net@gmail.com> wrote:
> Hey Kilian,
>
> Note that in IPython, references are held to objects in lots of places:
>
> In [23]: class C(object):
>    def __del__(self):
>        print 'deleting'
>   ....:
>   ....:
>
> In [26]: c=C()
>
> In [27]: c
> Out[27]: <__main__.C object at 0x862262c>
>
> In [28]: %reset
> Once deleted, variables cannot be recovered. Proceed (y/[n])?  y
>
> The deletion hasn't happened yet!
>
> We have to try harder:
>
> In [30]: del _27
>
> In [31]: del Out[27]
>
> In [32]: del ___
>
> In [33]: import gc
>
> In [34]: gc.collect()
> Out[34]: 92
> deleting
>
> Finally!

thanks for the explanation. I have a better idea now, where to find
possible references to my objects and how to get rid of them. I wasn't
aware of the %reset function and I didn't know that I have to call
gc.collect() manually (i thought that python takes care of the garbage
collection automatically).

There is still something I find puzzling in ipython's behavior when I
run a script using %run as opposed to running it interactively. If I
run the following script in ipython using the %run command, afterwards
I have a reference to the object c in the interactive session.
However, I can't figure out how to get rid of all references (without
calling %reset):

kilian@chebang:~$ cat test_destructor.py
class C(object):
    def __del__(self):
        print 'deleting object...'

c = C()

kilian@chebang:~$ python test_destructor.py
deleting object...

now, let's try in ipython:

In [1]: run test_destructor.py

In [2]: del c

In [3]: import gc

In [4]: gc.collect()
Out[4]: 47

(object still not deleted)

In [5]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])?  y
deleting object...

Finally!

Do you know, where I can find the additional reference to object c?
Let me explain why I need this: I am wrapping a python object around a
big chunk of memory using ctypes. I would like to de-allocate the
memory when the python object is deleted and was hoping to achieve
that with the __del__ method. This works fine within scripts that are
executed non-interactively, but when I use interactive ipython
sessions, I fill up my memory very fast.

> So, in addition to using %reset and gc.collect(), I'd encourage you to
> file a ticket for us to make %reset more robust.  It really needs to
> go deeper in cleanup mode.

I will do that.

> Finally, you must keep in mind that __del__ methods aren't guaranteed
> to be called, even absent ipython quirks.  See
>
> http://docs.python.org/ref/customization.html
>
> for details.

I was aware that __del__ is not guaranteed to be executed for objects
that are still around when the python process exits. But this is not a
problem in my case since I only use it to free allocated memory and
when the python process exits, this memory will be freed anyway.

cheers,
  kilian


More information about the IPython-user mailing list