Java Memory Management
Garbage Collection is the process of freeing space in the heap for the allocation of new objects. One of the best features of Java is automatic garbage collection.
Garbage Collector is the program running in the background that looks into all the objects in the memory and find out objects that are not referenced by any part of the program. All these unreferenced objects are deleted and space is reclaimed for allocation to other objects. One of the basic ways of garbage collection involves three steps:
Marking: This is the first step where the garbage collector identifies which objects are in use and which ones are not in use
Normal Deletion: Garbage collector removes the unused objects and reclaims the free space to be allocated to other objects
Deletion with compacting: For better performance, after deleting unused objects, all the survived objects can be moved to be together. This will increase the performance of allocation of memory to newer objects
Java Memory Management.
The JVM Heap is physically divided into two parts.
1. Young Gen:
- part of the heap reserved for the allocation of new objects.
- It is divided into three parts – Eden Memory and two Survivor Memory spaces.
- When the Young space becomes full, garbage is collected by running a special young collection, where all the objects that have lived long enough in the Young gen are moved to the old space, thus freeing up the young space for more object allocation. This garbage collection is called Minor GC.
Important points about the Young gen:
- Most of the newly created objects are located in the Eden Memory space
- When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces
- Minor GC also checks the survivor objects and moves them to the other survivor space. So at a time, one of the survivor space is always empty
- Objects that have survived many cycles of GC, are moved to the old generation memory space. Usually, it is done by setting a threshold for the age of the nursery objects before they become eligible to promote to the old generation
2. Old Gen:
Usually, garbage collection is performed in Old generation memory when it’s full. Old generation garbage collection is called as Major GC and usually takes longer.
Perm Gen:
“Perm Gen” contains the application metadata required by the JVM to describe the classes and methods used in the application.
Perm Gen also contains Java SE library classes and methods. Perm Gen objects are garbage collected in a full garbage collection.
Metaspace:
With Java 8, there is no Perm Gen, which means there is no more “java.lang.OutOfMemoryError: PermGen” space problems.
Unlike Perm Gen which resides in the Java heap, Metaspace is not part of the heap.
VM Switch | VM Switch Description |
–Xms | For setting the initial heap size when JVM starts |
-Xmx | For setting the maximum heap size |
-Xmn | For setting the size of young generation, rest of the space goes for old generation |
-XX:PermGen | For setting the initial size of the Permanent Generation Memory |
-XX:MaxPermGen | For setting the maximum size of Perm Gen |
-XX:SurvivorRatio | For providing ratio of Eden space, for example, if young generation size is 10m and VM switch is –XX:SurvivorRatio=2 then 5m will be reserved for Eden space and 2.5m each for both the Survivor spaces. The default value is 8 |
-XX:NewRatio | For providing ratio of old/new generation sizes. The default value is 2 |
Difference between PermGen Space and MetaSpace.
S.No | Key | PermGen | MetaSpace |
---|---|---|---|
1 | Basic | PermGen is the memory area for storing class data like static variable,byte code and etc | In Java 8, PermGen method area replaced with MetaSpace |
2 | Default Memory Allocation | By default 64 Mb is allocated for PermGen | It can by default auto increases its size |
3 | Tuned-up Memory Flag | It can be tuned by using -XXMaxPermSize. | We can restrict upper bound of the memory by -XX:MaxMetaspaceSize |
4 | Memory Area | It is a special Heap space. | Since Java 8, It is now separate memory area in the native OS |
Monitoring of Memory Use and GC Activity:
- Utilization of the different memory pools (Eden, Survivor and old generation). Memory shortage is the number-one reason for increased GC activity.
- If overall memory utilization is increasing continuously despite garbage collection, there is a memory leak, which will inevitably lead to an out-of-memory. In this case, a memory heap analysis is necessary
- The number of young generation collections provides information on the churn rate (the rate of object allocations). The higher the number, the more objects are allocated. A high number of young collections can be the cause of a response-time problem and of a growing old generation (because the young generation cannot cope with the quantity of objects anymore)
- If the utilization of the old generation fluctuates greatly without rising after GC, then objects are being copied unnecessarily from the young generation to the old generation. There are three possible reasons for this: the young generation is too small, there is high churn rate or there is too much transactional memory usage
jstat
The jstat utility uses the built-in instrumentation in the Java HotSpot VM to provide information about the performance and resource consumption of running applications. The tool can be used when diagnosing performance issues and in particular, issues related to heap sizing and garbage collection.
The jstat utility does not require the VM to be started with any special options. The built-in instrumentation in the Java HotSpot VM is enabled by default. This utility is included in the JDK download for all operating systems. The jstat utility uses the virtual machine identifier (VMID) to identify the target process.
Using jstat command with gc option to find out the JVM Heap Memory usage.
<JAVA_HOME>/bin/jstat –gc <JAVA_PID>
S0C | Current survivor space 0 capacity (KB) |
S1C | Current survivor space 1 capacity (KB) |
S0U | Survivor space 0 utilization (KB) |
S1U | Survivor space 1 utilization (KB) |
EC | Current eden space capacity (KB) |
EU | Eden space utilization (KB) |
OC | Current old space capacity (KB) |
OU | Old space utilization (KB) |
MC | Metasapce capacity (KB) |
MU | Metaspace utilization (KB) |
CCSC | Compressed class space capacity (KB) |
CCSU | Compressed class space used (KB) |
YGC | Number of young generation garbage collection events |
YGCT | Young generation garbage collection time |
FGC | Number of full GC events |
FGCT | Full garbage collection time |
GCT | Total garbage collection time |
Comments
Post a Comment