What does Java Garbage Collector do?
- Finds out the garbage objects from Heap space.
- Cleans up the garbage objects and retrieve the heap memory
In the early Java, application codes were not able to involve into what GC does. However, after JDK 1.2, java.lang.ref enabled the developers to interact with GC with limited access.
Garbage Collector and Reachability
If there is a valid reference, an object is considered “reachable“. GC scans objects that are unreachable and regard them as garbages that will be targeted to be cleaned up.
One reference object can refer other object and the other object and so on. This creates a reference chain, and the very first reference object is considered to be “root set“.

JVM runtime data area is divided as 3 different parts,
- Thread area
- Heap area: Stores the created objects
- Method are: Stores the class information
As GC mainly cleans up the garbages on the Heap area, let’s look deeper at the Heap area.
There are 4 different references that refers to the objects in the Heap area.
- Object inside the Heap area refers to another one in Heap space.
- Reference parameters inside the Java Stacks.
- e.g) local variables inside methods or method parameters
- Reference parameters inside the Native Stack(JNI).
- Static variables from Method Area.
Among the above 4 types of references, the 2,3,4 reference types are root set which decides the reachability.

As seen from the above Figure 2, objects that started to be referred from the Root Set objects are Reachable Objects while the others are Unreachable Objects that will be collected by GC.
The reference objects from the Figure 2 are the ones that do not use java.lang.ref package, and considered as strong reference.

Leave a comment