[Ironpython-users] Some questions about gc and finalizers

Jeff Hardy jdhardy at gmail.com
Thu Sep 18 14:35:30 CEST 2014


(+dinov, who just needs to page this in rather than learn it all from scratch)

On Wed, Sep 17, 2014 at 2:18 PM, Stefan Richthofer
<Stefan.Richthofer at gmx.de> wrote:
> Dear IronPython community,
>
> I recently worked on Jython issue 1057 (http://bugs.jython.org/issue1057) and also improved the current solution of http://bugs.jython.org/issue1634167 a bit. For these issues, it is very hard for Jython to emulate CPython behavior due to the fundamentally different GC implementation, so I suspected, IronPython might have similar problems and wondered how they are solved here.
> Since I never used IronPython or .net, I would appreciate answers on a rather abstract level and apologize that I have no ambition to look into the source myself. I'm just hoping to find someone (optimally a core-dev), who can simply answer it and is maybe open for a discussion of solutions.
>
> My questions are:
> - does IronPython support acquired finalizers?
> i.e.
> class A():
>   pass
>
> def A__del__():
>     print "A finalized"
>
> a1 = A()
> A.__del__ = A__del__
> a1 = None
>
>
> Would it output "A finalized" or not?

A quick test (on Mono) says "no", but trying with a class that
declares __del__ doesn't either, so I'd want to hook up a debugger on
Windows to check for sure. I'm not sure if the backing types created
by IronPython have finalizers, but I think not (and reading through
the RefEmit code will take longer than I have right now.)

> In Jython this won't work so easy, because Jython avoids to overwrite the finalize method for all instances for its expensiveness. So only instances known to need finalization on creation time will be finalized. AfaIk, finalizers in .net are as expensive as in Java, so how would this work in IronPython?
>
>
> - how complete is the support of the gc module?
> i.e.
> In Jython the support is rather poor; most methods are just implemented as stubs, i.e. one-liners as
>    throw Py.NotImplementedError("not applicable to Java GC")
>

IronPython is about the same:
https://github.com/IronLanguages/main/blob/ipy-2.7-maint/Languages/IronPython/IronPython.Modules/gc.cs

> gc.collect usually returns the number of collected objects, but Jython just calls java.lang.System.gc() and returns None. I believe, tracking the number of collected objects would be possible, but very expensive, so this maybe could be added as a start-up-flag feature for debugging. How far does IronPython support this currently?

IronPython just returns the number of bytes freed by the call:
https://github.com/IronLanguages/main/blob/ipy-2.7-maint/Languages/IronPython/IronPython/Runtime/PythonContext.cs#L3465.

- Jeff


More information about the Ironpython-users mailing list