Re: does python have useless destructors?

From: Ype Kingma (ykingma_at_accessforall.nl)
Date: 06/15/04


Date: Tue, 15 Jun 2004 23:20:15 +0200

David Turner wrote:

> "Delaney, Timothy C (Timothy)" <tdelaney@avaya.com> wrote in message
news:<mailman.957.1087256687.6949.python-list@python.org>...
>> David Turner wrote:
>>
>> > This will be a pain for the Jython implementers. However, it is
>> > doable. Note that I never said the objects couldn't be garbage
>> > collected, just that del had to be called at certain well-defined
>> > times. What this will involve is the Jython compiler inserting a lot
>> > of implicit try/finally constructs.
>> >
>> > Can anyone see a reason why this scheme wouldn't work?
>>
>> Yes - Jython cannot know about references to objects created *in Java
>> code*. It is therefore impossible for Jython to maintain reference
>> counts when an object is passed to Java (i.e. non-Python) code.
>>
>
> Thank you, Tim, that's the first substantial objection I've heard :-).
>
> So, if Java code makes use of a Python object with __del__, all bets
> are off. This is certainly a limitation, but I'm not sure that it's a
> very serious one, as RAII objects tend to be used in localized
> contexts.

It's not that all bets are off. In Jython the __del__() method is called
from java.lang.Object.finalize(), so you can place your bets there:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize()

This javadoc begins with:
"Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object."

The closest thing to a guarantee is to explicitly call the garbage
collector:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#gc()

>From Jython:

import java
java.lang.System.gc()

I wouldn't bet against this, ie. the last time I tried the first call
to this gc() called all __del__() methods instantly on non referenced
Python objects.

>
> I think the only available behaviour is simply to ignore the
> possibility of an object being referenced externally. Let Java code
> use Java idioms (try/finally).

For the moment, try/finally should also be a CPython idiom because
the call to __del__() is not guaranteed there either.

Kind regards,
Ype
 

-- 
email at xs4all.nl


Relevant Pages

  • Re: Where do they go?
    ... called References. ... builds up a layer of broken objects lying around the floor. ... But in Java, we have a Garbage Collector. ...
    (comp.lang.java.programmer)
  • Re: Are there any Garbage Collector improvements?
    ... If Mark is working with a Java project with very many tree-rings in it, it's quite likely the thing was coded exactly as horribly as possible from the stand-point of GC optimization, because of GC-optimization. ... Another good reason is that treating most objects as disposable can greatly simplify a lot of the logic, getting rid of pools and other scaffolding and allowing some of them to be made immutable, which lets you get rid of setters and possibly a lot of other code. ... To use a worn-out old example, you can get rid of all those xs and ys worked on in tandem and use Point2D or Complex or whatever fits the situation without all those used-up Point2Ds or Complexes gumming up the garbage collector the way they would have, five or ten years ago. ...
    (comp.lang.java.programmer)
  • Re: Why C# and Java have got it wrong
    ... Do you have any references to the claim that there is a fixation ... on garbage collection? ... that there have been "don't need to manage my resources" evangelizing ... >Java and C# actually provide poor tools for resource management. ...
    (comp.programming)
  • Re: Game programming -- Java performance?
    ... references to one another as a) indices, names, or other things and b) ... Murphy's Laws of java programming: ... Haha, well, you should probably try to find out why (perhaps step through with a debugger, or look at the source code?) ... If the app seems to have hung and you're sure it's an infinite loop ...
    (comp.lang.java.programmer)
  • Re: Return local object from function
    ... I thought that Java references were 64 bits (8 ... a pointer and there's therefore more going on in there than a simple ... contiguous in memory within another object. ...
    (comp.lang.java.programmer)

Loading