Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java's garbage-collected heap

An introduction to the garbage-collected <br>heap of the Java virtual machine

  • Print
  • Feedback

Page 3 of 7

In a JVM the root set is implementation dependent but would always include any object references in the local variables. In the JVM, all objects reside on the heap. The local variables reside on the Java stack, and each thread of execution has its own stack. Each local variable is either an object reference or a primitive type, such as int, char, or float. Therefore the roots of any JVM garbage-collected heap will include every object reference on every thread's stack. Another source of roots are any object references, such as strings, in the constant pool of loaded classes. The constant pool of a loaded class may refer to strings stored on the heap, such as the class name, superclass name, superinterface names, field names, field signatures, method names, and method signatures.

Any object referred to by a root is reachable and is therefore a live object. Additionally, any objects referred to by a live object are also reachable. The program is able to access any reachable objects, so these objects must remain on the heap. Any objects that are not reachable can be garbage collected because there is no way for the program to access them.

The JVM can be implemented such that the garbage collector knows the difference between a genuine object reference and a primitive type (for example, an int) that appears to be a valid object reference. However, some garbage collectors may choose not to distinguish between genuine object references and look-alikes. Such garbage collectors are called conservative because they may not always free every unreferenced object. Sometimes a garbage object will be wrongly considered to be live by a conservative collector, because an object reference look-alike refered to it. Conservative collectors trade off an increase in garbage collection speed for occasionally not freeing some actual garbage.

Two basic approaches to distinguishing live objects from garbage are reference counting and tracing. Reference counting garbage collectors distinguish live objects from garbage objects by keeping a count for each object on the heap. The count keeps track of the number of references to that object. Tracing garbage collectors, on the other hand, actually trace out the graph of references starting with the root nodes. Objects that are encountered during the trace are marked in some way. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.

Reference counting collectors

Reference counting was an early garbage collection strategy; here a reference count is maintained for each object. When an object is first created its reference count is set to one. When any other object or root is assigned a reference to that object, the object's count is incremented. When a reference to an object goes out of scope or is assigned a new value, the object's count is decremented. Any object with a reference count of zero can be garbage collected. When an object is garbage collected, any objects that it refers to has their reference counts decremented. In this way the garbage collection of one object may lead to the subsequent garbage collection of other objects.

  • Print
  • Feedback

Resources
  • "The Java Virtual Machine Specification," the official word from Sun, can be downloaded in PDF, HTML, and PostScript format.
    http://java.sun.com/doc
  • When it comes out, the book The Java Virtual Machine Specification, by Tim Lindholm and Frank Yellin (ISBN 0-201-63452-X), part of The Java Series, from Addison-Wesley, will likely be the best JVM reference.
    http://www.aw.com/cp/lindholm-yellin.html
    http://www.aw.com/cp/javaseries.html
  • Andrew W. Appel, "Garbage collection," in P. Lee (ed.), Advanced Language Implementations, MIT Press, 1991, chapter 4, pp. 89-100.
  • Jacques Cohen, 'Garbage collection of linked data structures', Computing Surveys, 13(3), 1981, pp. 341-367.
  • Paul R. Wilson, "Uniprocessor garbage collection techniques," Yves Bekkers and Jacques Cohen (eds.), Memory Management - International Workshop IWMM 92, St. Malo, France, September 1992, pp. 1-42.
  • June, 1996 Under The HoodThe lean, mean virtual machine -- Gives an introduction to the Java virtual machine. Look here to see how the garbage-collected heap fits in with the other parts of the JVM.
  • July, 1996 Under The HoodThe Java class file lifestyle -- Gives an overview to the Java class file, the file format into which all Java programs are compiled.