garbage collection : Java Glossary

go to home page G words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
garbage collection
In Java you explicitly create new objects with new, but you don’t need to explicitly free them. From time to time the garbage collector chases all the references in all the objects to find all the live objects. Anything that can’t be reached is dead and its space in reclaimed in one fell swoop. To nearly everyone’s great surprise, the more dead objects there are, the more efficient automatic garbage collection becomes relative to the explicit schemes used in C++. Unfortunately, automatic garbage collection is not as efficient in its use of RAM as explicit freeing because dead objects are not immediately detected. Automatic garbage collection has the big advantage you can’t screw it up.

With explicit freeing, you can accidentally free an object while some other reference is still pointing to it. Or you can forget to free it, and eventually clog memory with unused objects. There is nothing to stop you from writing your own explicit free allocators in Java that recycle objects in preference to creating new ones. These sorts of custom allocator would work well when objects are a standard size, when you don’t build complex references to these objects, when the objects are short lived, when RAM is tight, and/or when there are large numbers of live objects at any one time.

There are many ways of classifying garbage collectors, e.g. conservative vs. fully accurate. Conservative collection assumes everything on the stack is a pointer, and tries to trace its descendants. It ends up accidentally treating ints as pointers, and needlessly locking dead objects in RAM. Fully accurate ones determine first which are pointers and which are ints and floats. There are three main problems with conservative collectors:

  1. They sometimes take longer since the fool around chasing chains of objects that are not really chains.
  2. They can cause objects to be held onto that are actually dead, thus tying up memory needlessly.
  3. They might in, pathological circumstances, corrupt memory when false objects are marked as live.

A simple mark/sweep garbage collector (such as used in JDKs 1.0 through 1.2) pauses from time to time to collect all the garbage. This creates a quite noticeable pause from time to time. A generational collector does the work in little bits more frequently. In JDK 1.4+ there is an optional concurrent geneartional collector that takes more overhead but does not pause. You can invoke in with -XX:+UseParNewGC on the java.exe command line.

The amount of ingenuity in the design of garbage collection algorithms is astounding.

If the garbage collector cannot free up any RAM it throws an OutOfMemoryError. By this point it is usually too late to do anything.

Methods you may find useful:

Jove, for example, used a precise, multi-threaded, generational garbage collector. Sun’s HotSpot claims to have a utterly state of the art garbage collector.

System.gc is very efficient if you call it when there are very few objects, because it works by finding all live objects. It does not matter how many dead ones there are. There are natural breaks in an application where you have just deleted masses of objects and are just about to create a bunch more. That is the ideal time to insert a System.gc.

Terminology

If you were to stop your program in mid flight, some of the objects will never be used again. Some of these, a garbage collector knows about. Some it does not. The set of all objects that will be used again are called the live objects. A supernatural being might know exactly which they were. A program that had been run previously with the exact same data with monitoring could in theory know which objects were live. A clever programmer just studying your code could predict some of them. If there were some way of perfectly determining the live objects, you could safely garbage collect the RAM from all the remaining objects, even if there were references to them. This is what C++ programmers attempt to do as daring feat of bravado every time they write a program that does manual garbage collection.

In practice you don’t know the live objects. You know the reachable objects, which are a much larger set. Consider all the Threads in your program and all the local variables in all the currently running methods in those threads. Chase all the references in the local variables to the corresponding objects. Then chase all the references in those objects. Repeat until you have found all possible objects than can be indirectly reached. These are the reachable objects you keep. Everything else gets garbage collected.

Some objects are reachable, but clearly will never be used again. Objects which are reachable and which plausibly will be used again are called active. This subtle distinction is best explained by an example:

Books

book cover recommend book⇒Garbage Collection : Algorithms for Automatic Dynamic Memory Management
 hardcover
ISBN13:978-0-471-94148-4clickcounter
ISBN10:0-471-94148-4clickcounter
publisher:John Wiley & Sons
published:1996-09-17
by:Richard Jones, Rafael D Lins
Expensive, but fun.
UK flag abe books.co.uk abe books.ca Canadian flag
UK flag amazon.co.uk. amazon.ca. Canadian flag
German flag abe books.de chapters.indigo.ca . Canadian flag
German flag amazon.de. abe books.com American flag
French flag abe books.fr amazon.com. American flag
French flag amazon.fr. barnes and noble.com American flag
Italian flag abe books.it powells.com American flag
Spanish flag iberlibro.com abe books anz Australian flag

Learning More

Sun’s JDK Technote Guide on Concurrent Garbage Collection (1 CPU) : available:
Sun’s JDK Technote Guide on Parallel Garbage Collection (2 CPUs) : available:

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.62] The information on this page is for non-military use only.
You are visitor number 26,758. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/garbagecollection.html J:\mindprod\jgloss\garbagecollection.html