[Ironpython-users] Some questions about gc and finalizers

Stefan Richthofer Stefan.Richthofer at gmx.de
Thu Sep 18 17:10:32 CEST 2014


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

Sounds like IronPython would not support __del__ at all. Is this a basic, maybe well-known issue or are finalizers principally supported, but currently broken?
I appended a unit test file and would like to know which of the tests exactly fail on IronPython. I will surely take the time to test this myself, but it might
take some days until I come to setting up mono and IronPython. And I thought you might be interested in these tests too ;-)

To support acquired finalizers, Jython would have to track all instances for each class. So it could update them if the class would acquire a finalizer.
Obviously this is insanely expensive for such a rare use-case, so I added a builtin method __ensure_finalizer__ to Jython, which allows - at least manually - to fix this.
One can tell already existing instances manually that their class acquired a finalizer (also works for new style classes):

class A():
   pass

 def A__del__():
     print "A finalized"

 a1 = A()
 A.__del__ = A__del__
 a1.__ensure_finalizer__()
 a1 = None

I have only rough knowledge of .net, but believe it behaves somewhat equal to Java regarding to gc and finalizers.
Since IronPython appears to have equal issues, I would offer guidance and discussion if you might want to port the Jython approach.
At least I wanted to make you aware of the __ensure_finalizer__()-method-manual-fix-approach and suggest that if IronPython would
ever implement an equal method to agree on a common name for it.

> IronPython is about the same:
> https://github.com/IronLanguages/main/blob/ipy-2.7-maint/Languages/IronPython/IronPython.Modules/gc.cs
Okay, I already expected something like this. I have rough plans to enhance the Jython gc module by a manual track-method.
For debugging one can add certain objects to it via gc.track(object). Then gc would monitor these and serve the original
gc module methods with this information (e.g. number of collected (i.e. monitored and collected) objects).
I just wanted to make you aware of this and suggest that if you might want to do an equal monitoring, to agree on a common
name for the manual track method too. However, I would resume this topic when I proceed with this.


- Stefan



> Gesendet: Donnerstag, 18. September 2014 um 14:35 Uhr
> Von: "Jeff Hardy" <jdhardy at gmail.com>
> An: "Stefan Richthofer" <Stefan.Richthofer at gmx.de>
> Cc: "ironpython-users at python.org" <ironpython-users at python.org>, "Dino Viehland" <dinov at microsoft.com>
> Betreff: Re: [Ironpython-users] Some questions about gc and finalizers
>
> (+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
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_finalizers.py
Type: text/x-java
Size: 12490 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20140918/77e4c299/attachment.java>


More information about the Ironpython-users mailing list