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.


  1. Thanks for helping me to understand Garbage Collection and its algorithms concepts. As a beginner in java programming your post help me a lot. java training in chennai

  2. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in TECHNOLOGY , kindly contact us
    MaxMunus Offer World Class Virtual Instructor-led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ pieces of training in India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Pratik Shekhar
    Ph:(0) +91 9066268701

  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

  4. Established in 2016 is providing top quality domain and hosting services worldwide to
    our valued customers and trying to play a little role in their success.We offer about 500 distinctive gTlds and ccTlds
    to look over which includes old master class gTlds like .com, .net and .org, in addition this we also offer newly launched Tlds
    like .xyz, .online, .master, .office, .on, .top and .club. We can assist you with choosing the best fitting name. Let's bring your
    thought or business on the web. visit this site to know more.

    Do you wanna buy SSD Web Hosting visit here.
    Find best Cheap Web Hosting here.