Unit 3 · OOP Concepts

3.2.2Garbage collection and the object lifecycle

Garbage collection

The garbage collector (GC) is part of the Java Virtual Machine. It finds objects that can no longer be reached and frees their memory.

An object becomes eligible for garbage collection when no live reference can reach it.

Java
Student a = new Student("Nimal", 80);   // object 1
Student b = new Student("Kamala", 60);  // object 2
a = b;        // object 1 now has no reference: eligible for GC
b = null;     // object 2 is still referenced by a: not eligible
a = null;     // now object 2 is eligible as well
after the first two linesabNimalKamalaa = b;abNimalKamalaGCb = null;abNimalKamalanullGCa = null;abNimalKamalanullnullGCGC
Garbage collection: an object becomes eligible as soon as no reference can reach it

Key points: you cannot force garbage collection; System.gc() is only a request. The programmer's job is to remove references that are no longer needed. Objects that refer only to each other (a circular reference) are still collected once the program can no longer reach any of them.

Three ways an object becomes unreachable

Java
// 1. The reference is set to null
Student a = new Student("Nimal", 80);
a = null;                        // the Nimal object can be collected

// 2. The reference is pointed at another object
Student b = new Student("Kamala", 60);
b = new Student("Sunil", 70);    // the Kamala object can be collected

// 3. The reference goes out of scope
static void makeTemp() {
    Student t = new Student("Temp", 0);
}                                // when makeTemp returns, t is gone,
                                 // so the Temp object can be collected

Only objects that cannot be reached are collected. The GC starts from the "roots" (the local variables in every stack frame, and static variables) and follows every reference. Any object it cannot reach this way is garbage.

When does the GC run?

The JVM decides. It usually runs when the heap is getting full. Modern collectors divide the heap into a young area for new objects and an old area for objects that have survived several collections. Most objects die young (think of the temporary objects created inside a loop), so cleaning the young area often and quickly is very efficient. You do not need to manage any of this, but it explains why you cannot predict when an object will actually be freed.

Java programs can still leak memory. If you keep adding objects to a list or a static collection and never remove them, they stay reachable and can never be collected. The garbage collector cannot know that you will never use them again. Remove references that you no longer need.

Use the stepper below to see the stack and heap change as references are moved.

Object lifecycle

  1. Creation – new allocates memory and the constructor initialises the object.
  2. Use – methods are called and attributes change.
  3. Unreachable – the last reference is removed or goes out of scope.
  4. Collection – the garbage collector reclaims the memory.
1Creation: new and the constructor→2Use: methods are called→3Unreachable: no reference left→4Collection: memory reclaimed

Resources outside memory (files, database connections) must be closed by you, using close() or try-with-resources (see 3.1.5).