Saturday, November 24, 2012

JVM Memory management: Garbage Collection



Most of the questions related to memory would have been answered by the image above. There are two and only two major partitions in JVM memory, Heap and Non-Heap. All the other type of memory space or pools we have heard belong to either of these two. Java specification does not enforce any specific implementation of memory, it depends on JVM implementation. The JVM of Oracle is named HotSpot.


Young Generation:
Here garbage collection is frequent and less expensive.
Eden Space: Initially all objects created here.
Survivor space: Contains objects that survived garbage collection in Eden Space

Old Generation:
Here garbage collection is less frequent and expensive.
Tenured Space: The objects survived the garbage collection get moved here.
PermGen: Contains meta data of the virtual machine, class and method objects.

Garbage Collector:
It is a low priority daemon thread. It frees memory of any object that is not reachable from a live thread. We can suggest JVM to do garbage collection by calling Runtime.getRuntime().gc() or System.gc() which internally calls Runtime.getRuntime().gc(). Runtime is singleton. We can configure type of garbage collector to be used by passing command line arguments to JVM. For more details on garbage collection http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html 

To configure stack memory  -Xss1m
To configure heap memory  -Xmx512m -XX:MaxPermSize=256m



No comments: