Wednesday, August 24, 2011
Understanding Java Garbage Collection and its algorithms.
Here is a deeper dive into - Understanding Java Garbage Collection and its algorithms.
This article is in continuation to my article on "Understanding Java Heap Space and Memory Tuning". Make sure you understand the concepts behind Java heap usage before diving into garbage collection details.
We cannot force Garbage Collection, but could request it by calling System.gc() or Runtime.gc().
JVM does not guarantee that GC will be started immediately. Also, Garbage collection does not guarantee that a program will not run out of memory.
JVM automatically removes the unused variables/objects from the memory... to create free space for the programs that are currently running.
Garbage Collection runs on Heap and NOT stack
The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected. We usually nullify the object references of large objects like collections, maps etc in the finalize block.
An object's finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object's finalize() method may be invoked by other objects.
An object's finalize() method may only be invoked once by the garbage collector.
A reachable object cannot be garbage collected. Only unreachable objects may be garbage collected. The system will not do it and you cannot force it either.
An unreachable object may become reachable again. This can happen when the object's finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.
GC is a daemon thread that runs in JVM
How does an object become eligible for GC
1. All available object references are null
2. When parent Object references become null, all child objects (objects references in other object) also become eligible for GC
3. Make the reference variable to refer to another object and neither of the objects is referenced by any other object. That's an island of isolation. Basically, an island of isolation is a group of objects that reference each other but they are not referenced by any active object in the application
Various types of Garbage collection Java which can be used by passing params to the JVM via executing a java program.
version of the young generation collector
collect the tenured generation
When GC happens, it may run parallel to the application or can pause the application for times when GC is done.
UseParallelGC : Young Generation is partially and parallel'ly collected. It is a minor collection & carried in parallel to executing code and is not much of a performance impact. When it performs a minor GC, the VM moves any remaining objects from the eden space to one of the survivor spaces. The VM moves objects that live long enough in the survivor spaces to the "tenured" space in the old generation.
UseConcMarkSweepGC : This is a full GC and application pauses while it runs. It is a performance heavy. When the old generation fills up, there is a full GC that is often much slower because it involves all live objects.