Return to Tutorial Index

Back to the home page of this site


Java2 Certification


You can discuss this topic with others at
Read reviews and buy a Java Certification book at

3)Garbage Collection

Objective 1)

State the behavior that is guaranteed by the garbage collection system and write code that explicitly makes objects eligible for collection.

Why would you want to collect the garbage?

You can be a very experienced Java programmer and yet may never had to familiarise yourself with the details of garbage collection. Even the expression garbage collection is a little bizarre. In this context it means the freeing up of memory that has been allocated and used by the program. When the memory is no longer needed it can be considered to be garbage, i.e. something that is no longer needed and is simply cluttering up the living space.

One of the great touted beauties of the Java language is that you don't have to worry about garbage collection. If you are from a Visual Basic background it may seem absurd that any system would not look after this itself. In C/C++ the programmer has to keep track of the allocation and deallocation of memory by hand. As a result "memory leaks" are a big source of hard to track bugs. This is one of the reasons that with some versions of Microsoft applications such as Word or Excel, simply starting and stopping the program several times can cause problems. As the memory leaks away eventually the whole system hangs and you need to hit the big red switch. Somewhere in those hundreds of thousands of lines of C++ code, a programmer has allocated a block of memory but forgot to ensure that it gets released.

Java and garbage

Unlike C/C++ Java automatically frees up unused references. You don't have to go through the pain of searching for allocations that are never freed and you don't need to know how to alloc a sizeof a data type to ensure platform compatibility. So why would you want to know about the details of garbage collection? Two answers spring to mind, one is to pass the exam and the other is to understand what goes on in extreme circumstances. 

If you write code that creates very large numbers of objects or variables it can be useful to know when references are released. 
If you read the newsgroups you will see people reporting occasions of certain Java implementations exhausting memory resources and falling over. This was not in the brochure from Sun when they launched Java. 

In keeping with the philosophy of automatic garbage collection, you can suggest or encourage the JVM to perform garbage collection but you can not force it. 

Let me re-state that point, you cannot force garbage collection, just suggest it. 

At first glance finalisation sounds a little like the destructors in C++ used to clean up resources before an object is destroyed. The difference is that Java internal resources do not need to be cleaned up during finalisation because the garbage collector looks after memory allocation. However if you have external resources such as file information, finalisation can be used to free external resources. 

Garbage collection is a tricky one to write exercises with or practice with as there is no obvious way to get code to indicate when it is available for garbage collection. You cannot write a piece of code with a syntax like

if(EligibleForGC(Object){ //Not real code
    System.out("Ready for Garbage");

Because of this you just have to learn the rules. To re-state.

Once a variable is no longer referenced by anything it is available for garbage collection.

You can suggest garbage collection with System.gc(), but this does not guarantee when it will happen

Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created. 


Question 1)

Which of the following is the correct syntax for suggesting that the JVM performs garbage collection?

2) System.setGarbageCollection();
3) System.out.gc();
4) System.gc();

Question 2)

What code can you write to ensure that the Integer variables are garbage collected at a particular point in this code?

public class Rub{
        Integer  i= new Integer(1);
        Integer  j=new Integer(2);
        Integer k=new Integer(3);
public static void main(String argv[]){
            Rub r = new Rub();
    public void amethod(){

1) System.gc();
3) Set the value of each int to null
4) None of the above


Answer to Question 1)

4) System.gc();

Answer to Question 2)

4) None of the above

You can only suggest garbage collection, therefore you cannot be certain that it will run at any particular point in your code. Note that only instances of classes are subject to garbage collection not primitives.

Other sources on this topic

An article from SUN

Jyothi Krishnan on this topic at

Last updated
13 Jan 1999
copyright © Marcus Green 1999