views:

1586

answers:

6

Hopefully a simple question. Take for instance a Circularly-linked list:

class ListContainer
{
  private listContainer next;
  <..>

  public void setNext(listContainer next)
  {
    this.next = next;
  }
}

class List
{
  private listContainer entry;
  <..>
}

Now since it's a circularly-linked list, when a single elemnt is added, it has a reference to itself in it's next variable. When deleting the only element in the list, entry is set to null. Is there a need to set ListContainer.next to null as well for Garbage Collector to free it's memory or does it handle such self-references automagically?

+4  A: 

Java collects any objects that are not reachable. If nothing else has a reference to the entry, then it will be collected, even though it has a reference to itself.

Dave L.
+11  A: 

Circular references is a (solvable) problem if you rely on counting the references in order to decide whether an object is dead. No java implementation uses reference counting, AFAIK. Newer Sun JREs uses a mix of several types of GC, all mark-and-sweep or copying I think.

You can read more about garbage collection in general at Wikipedia, and some articles about java GC here and here, for example.

gnud
Jikes RVM includes a high performance reference counting GC. See http://cs.anu.edu.au/~Steve.Blackburn/pubs/papers/urc-oopsla-2003.pdf for details.
Luke Quinane
+16  A: 

Garbage collectors which rely solely on reference counting are generally vulnerable to failing to collection self-referential structures such as this. These GCs rely on a count of the number of references to the object in order to calculate whether a given object is reachable.

Non-reference counting approaches apply a more comprehensive reachability test to determine whether an object is eligible to be collected. These systems define an object (or set of objects) which are always assumed to be reachable. Any object for which references are available from this object graph is considered ineligible for collection. Any object not directly accessible from this object is not. Thus, cycles do not end up affecting reachability, and can be collected.

See also, the Wikipedia page on tracing garbage collectors.

jsight
Any reference counting GC with a cycle detector (such as trial deletion or mark/sweep) will also catch self references.
Luke Quinane
+6  A: 

The actual answer to this is implementation dependent. The Sun JVM keeps track of some set of root objects (threads and the like), and when it needs to do a garbage collection, traces out which objects are reachable from those and saves them, discarding the rest. It's actually more complicated than that to allow for some optimizations, but that is the basic principle. This version does not care about circular references: as long as no live object holds a reference to a dead one, it can be GCed.

Other JVMs can use a method known as reference counting. When a reference is created to the object, some counter is incremented, and when the reference goes out of scope, the counter is decremented. If the counter reaches zero, the object is finalized and garbage collected. This version, however, does allow for the possibility of circular references that would never be garbage collected. As a safeguard, many such JVMs include a backup method to determine which objects actually are dead which it runs periodically to resolve self-references and defrag the heap.

James
+2  A: 

Simply, Yes. :)

Check out http://www.ibm.com/developerworks/java/library/j-jtp10283/

All JDKs (from Sun) have a concept of "reach-ability". If the GC cannot "reach" an object, it goes away.

This isn't any "new" info (your first to respondents are great) but the link is useful, and brevity is something sweet. :)

sam
+4  A: 

As a non-answer aside (the existing answers more than suffice), you might want to check out a whitepaper on the JVM garbage collection system if you are at all interested in GC. (Any, just google JVM Garbage Collection)

I was amazed at some of the techniques used, and when reading through some of the concepts like "Eden" I really realized for the first time that Java and the JVM actually could beat C/C++ in speed. (Whenever C/C++ frees an object/block of memory, code is involved... When Java frees an object, it actually doesn't do anything at all; since in good OO code, most objects are created and freed almost immediately, this is amazingly efficient.)

Modern GC's tend to be very efficient, managing older objects much differently than new objects, being able to control GCs to be short and half-assed or long and thorough, and a lot of GC options can be managed by command line switches so it's actually useful to know what all the terms actually refer to.

Note: I just realized this was misleading. C++'s STACK allocation is very fast--my point was about allocating objects that are able to exist after the current routine has finished (which I believe SHOULD be all objects--it's something you shouldn't have to think about if you are going to think in OO, but in C++ speed may make this impractical).

If you are only allocating C++ classes on the stack, it's allocation will be at least as fast as Java's.

Bill K
Hi! Could you explain a bit more regarding what you mean by "When Java frees an object, it actually doesn't do anything at all; since in good OO code, most objects are created and freed almost immediately"? Thanks :)
The first part of the GC area is divided into 2 parts called "Eden". Only 1/2 is used at a time. All it does is fill it from bottom to top. When that area is full, anything that has been de-allocated has been completely forgotten at this point--anything remaining is copied over to the other half and then the first half is considered empty and the other half starts to fill. When an object looks like it'll be around for a while, java moves it from eden to another area and uses a completely different mechanism to track it.
Bill K
This means that allocating and freeing small objects is about as expensive as C's stack allocation which also doesn't have a specific step for "Freeing". The nice part about Java is the exact same mechanism works for both short-term and long-term objects... The runtime figures out which is appropriate and when to use it, so it can analyze the code and figure out which should go where as quickly as necessary. GC can be finely tuned as well by command-line options.
Bill K