Most Popular Posts

16/04/2013

Garbage Collection (GC)


Garbage Collection is integral part of the JVM (Java Virtual Machine) and it is used to reclaim memory occupied by the unused objects.

When the application runs, it creates objects. Once the object is created it occupies some memory. When the object is not used/ referenced it can be considered as unused and will remain in the memory until the garbage collector claims the memory occupied by this object.

Objects at first are created and put in the Young Generation space. After some time can be moved (Partial/ Minor Garbage Collection) into the Old Generation space. Once the Young and Old Generation is FULL, Full Garbage Collection is run. 

This operation might impact the flow of the application, as it might take a very long time to complete. During this operation application becomes unresponsive.

To avoid this, we can tune the JVM Heap/ other settings. Different JVMs have different GC policy algorithms which will be discussed in the next posts.



Examples:

If we add the following option to the command line for the JVM:
-verbose:gc
we will get the information about the heap and result of the garbage collection for each collection.

[GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs]
In this scenario we can see two minor GC and one Full/ Major GC.

  • First numbers stand for the size of live objects before and after the GC.
  • Numbers in parentheses represent memory space usable for java objects (perm/ survivor spaces not included).
  • Secs stands for the amount of time in seconds that was taken to perform the action
[GC [DefNew: 64575K->959K(64576K), 0.0457646 secs] 196016K->133633K(261184K), 0.0459067 secs]
Here the second numbers represent the heap usage reduction (196016K -> 133633K(261184K)



If we want to get even more details on the GC results we can add the following options:

-XX:+PrintGCDetails

If we add:

-XX:+PrintGCTimeStamps

we will get additional information regarding timestamps when the GC occurred  Based on such results we can see how often GC occurs.

No comments:

Post a Comment