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 2 of 7

A second advantage of garbage collection is that it helps ensure program integrity. Garbage collection is an important part of Java's security strategy. Java programmers are unable to accidentally (or purposely) crash the JVM by incorrectly freeing memory.

A potential disadvantage of a garbage-collected heap is that it adds an overhead that can affect program performance. The JVM has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly. This activity will likely require more CPU time than would have been required if the program explicitly freed unnecessary memory. In addition, programmers in a garbage-collected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed.

Fortunately, very good garbage collection algorithms have been developed, and adequate performance can be achieved for all but the most demanding of applications. Because Java's garbage collector runs in its own thread, it will, in most cases, run transparently alongside the execution of the program. Plus, if a programmer really wants to explicitly request a garbage collection at some point, System.gc() or Runtime.gc() can be invoked, which will fire off a garbage collection at that time.

The Java programmer must keep in mind that it is the garbage collector that runs finalizers on objects. Because it is not generally possible to predict exactly when unreferenced objects will be garbage collected, it is not possible to predict when object finalizers will be run. Java programmers, therefore, should avoid writing code for which program correctness depends upon the timely finalization of objects. For example, if a finalizer of an unreferenced object releases a resource that is needed again later by the program, the resource will not be made available until after the garbage collector has run the object finalizer. If the program needs the resource before the garbage collector has gotten around to finalizing the unreferenced object, the program is out of luck.

Garbage collection algorithms

A great deal of work has been done in the area of garbage collection algorithms. Many different techniques have been developed that could be applied to a JVM. The garbage-collected heap is one area in which JVM designers can strive to make their JVM better than the competition's.

Any garbage collection algorithm must do two basic things. First, it must detect garbage objects. Second, it must reclaim the heap space used by the garbage objects and make it available to the program. Garbage detection is ordinarily accomplished by defining a set of roots and determining reachability from the roots. An object is reachable if there is some path of references from the roots by which the executing program can access the object. The roots are always accessible to the program. Any objects that are reachable from the roots are considered live. Objects that are not reachable are considered garbage, because they can no longer affect the future course of program execution.

  • 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.