Python vs Java garbage collection?

Delaney, Timothy tdelaney at avaya.com
Sun Jan 5 22:36:00 EST 2003


> From: maney at pobox.com [mailto:maney at pobox.com]
> 
> I don't understand what you're saying here at all.  Do you really mean
> that a Java programmer *cannot* implement reference counting in a way
> analogous to what can be done in, say, C++?  Of course there 
> are issues
> about letting references escape into non-counted contexts, and in Java
> one wouldn't be counting for the purpose of scavenging memory.

Consider the following case. Assume that Jython implements reference
counting on the Python side.

    import java.lang

    a = [1]
    b = java.lang.LinkedList()
    b.append(a)
    del a
    print b

At the `print b`, what state is b in? Finalisation has already been called
on the list that it contains as an element, because the java VM knows
absolutely nothing about reference counts. So when it tries to print it,
what happens? I don't know.

It's not just about memory scavenging. Any time a reference escaped into a
non-controlled environment things are likely to screw up.

The reason that CPython is able to deal with reference counts is because
meticulous care is taken in the core python VM code and extensions to
maintain the reference counts.

Tim Delaney





More information about the Python-list mailing list