killian koepsell | 13 Jul 09:04

question about garbage collection

Hi,

I noted the following behavior in ipython: If you run the following
few lines in ipython, an object is created and destroyed:

In [1]: class C(object):
  ...:     def __del__(self):
  ...:         print 'deleting object...'
  ...:
  ...:

In [2]: obj = C()

In [3]: del obj
deleting object...

The same, of course, works if you run theses line within a script that
is called from ipython. If you don't delete the
object at the end of the script, it is available afterwards in
ipython. For some reason, the object destructor doesn't
seem to be called when the object is later deleted. It seems that even
after deleting the object, there is still some
reference to that object around somewhere. Is this the expected/wanted
behavior? Is it possible to force a complete
destruction of the object in order to free the used memory?

Thanks,
 Kilian
Fernando Perez | 13 Jul 09:48

Re: question about garbage collection

Hey Kilian,

On Sun, Jul 13, 2008 at 12:08 AM, killian koepsell <koepsell <at> gmail.com> wrote:
> Hi,
>
> I noted the following behavior in ipython: If you run the following
> few lines in ipython, an object is created and destroyed:
>
> In [1]: class C(object):
>  ...:     def __del__(self):
>  ...:         print 'deleting object...'
>  ...:
>  ...:
>
> In [2]: obj = C()
>
> In [3]: del obj
> deleting object...
>
> The same, of course, works if you run theses line within a script that
> is called from ipython. If you don't delete the
> object at the end of the script, it is available afterwards in
> ipython. For some reason, the object destructor doesn't
> seem to be called when the object is later deleted. It seems that even
> after deleting the object, there is still some
> reference to that object around somewhere. Is this the expected/wanted
> behavior? Is it possible to force a complete
> destruction of the object in order to free the used memory?

If your object isn't getting deleted, likely it's because there are
(Continue reading)

killian koepsell | 13 Jul 21:28

Re: question about garbage collection

Hi Fernando,

On Sun, Jul 13, 2008 at 12:48 AM, Fernando Perez <fperez.net <at> 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 ___
(Continue reading)

Fernando Perez | 16 Jul 09:15

Re: question about garbage collection

Hey,

On Sun, Jul 13, 2008 at 12:28 PM, killian koepsell <koepsell <at> gmail.com> wrote:

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

It does, but gc.collect() may happen at some arbitrary later time that
you have no control over.  The reason you can call it is that
sometimes you have a good reason to need a full collection cycle to be
run *now*, not when the VM gets around to it.

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

There is in principle no automatic way of getting all object
references in python.  However, since most important variables you
care about live in a dict called _ip.user_ns, you could write a bit of
code to build the reverse mapping to find all the references bound to
a given object in that namespace:

refs = {}
for k in _ip.user_ns.keys():
(Continue reading)


Gmane