Re: Garbage collection

From: Chris Uppal (chris.uppal_at_metagnostic.REMOVE-THIS.org)
Date: 03/30/04


Date: Tue, 30 Mar 2004 16:07:46 +0100

Josef Garvi wrote:
> Tony Morris wrote:
> > This is implementation dependant behaviour.
>
> Are you sure about this? (The other replies to my post seem to think
> differently). But if so, it has quite some implications for code writing.

Let's start with the bottom line: you don't have to worry about using circular
references, they will be cleaned up for you just fine.

However, Tony is right and this *is* implementation dependent.

What you have to realise is that there are *no* guarantees about how the GC
works, when it will run, or even whether there *is* a garbage collector. The
only thing that the spec tells you is that space won't be re-used while an
existing (reachable) object still needs it.

But there's more to it than just the spec. You also have to consider whether a
JVM implementation is usable for a specific purpose. Most JVMs are intended to
be general purpose, so they *have* to use a GC implementation that is able to
reclaim reference cycles. And they do. All of them.

So why go on about it being implementation dependent ? There are two points
here.

One is that you can't, or shouldn't, generalise from what one implementation
does to what *all* implementations do. For instance a JVM designed for hard
real-time processing might (at least as an option) not have GC at all (on the
grounds that it wouldn't be needed, and only slows things down and takes up
space). Another JVM implementation might use (only) reference counting if it
were targeted at applications that needed good incremental GC but never
generated significant reference cycles. The JVM implementer has a good deal of
freedom here as far as the spec goes. In practice of course, such
special-purpose JVMs are going to be the exception rather than the rule, and
ones that break the customary assumptions (even assumptions that aren't defined
as part of the spec) will make it very clear that they don't work in the
"normal" way.

The other point is that GC is a complex and delicate science. The state of the
art continues to advance, and what is true today may not be true tomorrow. The
Java spec (just for once) is sensible about this and doesn't attempt to pin
down implementations to techniques that might be superseded in the next few
years. As an example of this, although pure reference counting can never be
appropriate for a general-purpose JVM, GC schemes that use a hybrid of
reference counting and "classic" GC seem to be coming back into fashion in GC
research. One potential effect of that is that reference cycles might be
collected less readily than non-cyclic references -- objects might be finalised
later if they were in part of cycles than if they were not.

You can never tell, the spec gives no guarantees. But if you *need* to know
then the chances are that that's because of a flaw in your own design. If not
then you just have to ensure that your code is running on a JVM that implements
GC in the way you depend on.

So, yes it is implementation dependent, but no, you don't need to worry about
it.

    -- chris



Relevant Pages

  • Re: A Java Brainteaser - a Static Factory method Narrative
    ... When a Java programmer invokes a System.gccall it ... >>> memory to the JVM at the earliest algorithmically determined time. ... Look at it this way - when a reference loses scope the JVM throws it ... >>> created by a factory class from a static method ever be clean ...
    (comp.object)
  • Re: A Java Brainteaser - a Static Factory method Narrative
    ... When a Java programmer invokes a System.gccall it ... >>> memory to the JVM at the earliest algorithmically determined time. ... Look at it this way - when a reference loses scope the JVM throws it ... >>> created by a factory class from a static method ever be clean ...
    (comp.lang.java.programmer)
  • Re: Garbage collection, threads and GUIs.
    ... Interrupting ... something out to stdout/err when it terminates. ... you only need to keep a reference ... The JVM keeps its own internal reference to all threads ...
    (comp.lang.java.help)
  • Re: Garbage Collection
    ... >>is up to the JVM to decide. ... > Be careful. ... that indicates that a JVM implementation is required ... to hold an inactive Thread reference in the ThreadGroup. ...
    (comp.lang.java.programmer)
  • Re: Advice needed on __del__
    ... phil writes: ... > reference to an instance is deled. ... That's fine, if consistent. ... Exactly when that is is implementation dependent. ...
    (comp.lang.python)