Bits of Java - Episode 3: Garbage Collection

In this episode we will talk about one of the features that distinguishes Java from a lot of other programming languages out there: the concept of garbage collection.

In other languages, such C for instance, you should take care of disposing objects that you do not need anymore, in order to avoid problems, the most common being memory leak.

Luckily for you, Java, and in particular the JVM in which your application runs, takes care by itself of getting rid of objects that are no longer used in the application. In particular, all Java objects are stored in the heap, which is like a store of unused memory space dedicated to your application, and, objects that are not needed anymore are removed form the heap to free memory.

But, what exactly does it mean for an object to not be used anymore? Well, you are probably familiar with the fact that when you create a variable of a reference type and you assign it to an object instance, what you are doing is to create a reference that points to a certain object which is stored in the memory. If, after that, you set the variable to null, you are cutting the link between the reference variable and the actual object.

Furthermore, you should know that variables have a particular scope within an application. For instance, a local variable defined within a method gets out of scope outside the method, while, for instance, a class variable remains in scope for the whole lifetime of the program.

So, given these two facts, we can say that an object is not used anymore when it has no more references pointing to it, or when all references pointing to it are gone out of scope. Let’s look at a few examples.

public class MyClass {
    String name;
    public MyClass(String newName) {
        name = newName;
    }
    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public static void main(String[] args) {
        /*
        * Here an object is created in memory and our variable reference to it
        */
        MyClass refVariable = new MyClass("Ilenia");
        String name = refVariable.getName();
        System.out.println(name);

        /*
        * Here we cut the link between the variable and the actual object,
        * so there are no more references to it.
        * The object becomes eligible for garbage collection
        */
        refVariable = null;
    }
}
public class MyClass {
    String name;
    public MyClass(String newName) {
        name = newName;
    }
    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public static void main(String[] args) {
        /*
        * Here an object is created in memory and our variable reference to it
        */
        MyClass refVariable1 = new MyClass("Ilenia");
        String name = refVariable.getName();
        System.out.println(name);

        /*
        * Now we have a second reference to the same object
        */
        MyClass refVariable2 = refVariable1;

        /*
        * Here we cut the link between the refVariable1 and the actual object,
        * but there is still refVariable2 which points to the same object, so the
        * object is still reachable
        */
        refVariable1 = null;

        /*
        * Here we are cutting the last link we had with the object in memory, 
        * making it eligible for garbage collection
        */
        refVariable2 = null;
    }
}

When one of the described conditions applies, Java realizes that the object is eligible for garbage collection. This, however, does not mean that the object is immediately removed from the heap. This can happen at any time once it has been marked as such.

Practically, you as developer have not much control over the garbage collection process. The only thing you can do to help is make sure you make an object eligible for garbage collection when it is no longer needed.

Java also provides a method System.gc(), which can be called at any time in your application, but which does not guarantee to actually do anything. It is like a suggestion to the JVM that the garbage collection can take place and memory can be freeing from the heap, but the JVM itself can also decide to ignore it.

So, just remember what it means for an object to be eligible for garbage collection and take care in your application to make an object as such when you do not need it anymore. The rest, luckily for us, is already handled by the JVM!

Next topic of the series will be Overflow and Underflow in Casting.

by Ilenia Salvadori