JVM: Memory management and Garbage collection

If you have looked into server configuration files, you would have seen terms like –

-Xms512m -Xmx1536m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=786m -XX:MaxPermSize=1024m -XX:+UseConcMarkSweepGC

These are parameters we use to set up heap memory use by JVM to store objects in memory and make sure effective usage of garbage collection.

Garbage Collection:
Garbage collection or GC in Java is a way in which JVM reclaims the space in heap occupied by objects no longer in use. This is achived by Java in Mark and Delete approach (Mark objects no longer in use and then delete the marked objects, optionally compacting the survived objects to make all available space together for better usage).

Heap Memory: All the objects created by JVM are added to heap memory. -Xms512m defines the minumum heap memory requested by JVM and -Xmx1536m defines maximum.

Yound and Old Generation: Further heap memory is divided into 2 blocks- young generation and old generation for optimum GC. Young generation memory contains the newly generated objects and old generation contains older objects. The idea is that the newly created objects will be more prone to GC. The objects which have survived a few GC cycles (they are still in use), have more probability of surviving future GC cycles. A minor GC runs frequently which will clean up the young generation heap. The major GC runs less frequently and cleans up whole healp including old generation area.

-XX:NewSize=512m and -XX:MaxNewSize=512m defines memory setting for young generation GC.

Eden and Survivor: Young generation heap area is further divided into Eden space and survivor space. A newly created object is placed in Eden space and moves to survivor space if it survives a GC. A survivor space object which has survived multiple GCs is then moved to Old Generation area.

Stack memory: Apart from Heap memory, JVM also has stack memory which is allocated to a thread of execution and store local primitive variable and reference to variables in heap for that particular thread. Stack memory works in LIFO fashion and is short lived. Normally it is very small in size ~1mb by default, but can be set using -Xss flag.

Error:
OutOfMemoryError is thrown by JVM when it runs out of heap and StackOverFlowError is thrown when stack memory is full.

Permanent Generation:
Perm Gen is area where JVM defines application metadata about classes and methods. This is not part of heap memory. Can be set using -XX:PermSize=786m and -XX:MaxPermSize=1024m

GC Algorithms:
There are various algorithms which can be used by JVM while GC. For example -XX:+UseConcMarkSweepGCin above cofiguration tells JVM to use concurrent mark sweep algo, which helps GC in parallel to application execution, hence low impact visible on application – read here. With Java 7, we have Garbage-First Collector option, which helps further by splitting heap in multiple areas and doing GC with minimum impact on application- read here.

Useful reads

http://pubs.vmware.com/vfabric52/index.jsp?topic=/com.vmware.vfabric.em4j.1.2/em4j/conf-heap-management.html

http://www.journaldev.com/2856/java-jvm-memory-model-and-garbage-collection-monitoring-tuning

https://blog.codecentric.de/en/2014/01/useful-jvm-flags-part-8-gc-logging/