Let us begin with the very basic question, why do we need our JVM tuned up? And what exactly is JVM.
JVM, is the instance of Java Runtime Environment, which comes into action whenever you run your application
So, in order to make sure JVM runs fine we require that there is enough space for JVM to run programs. And hence we require timely check on the performance factors and space allotted to the JVM.
The crux of the matter: Heap and Garbage Collection
The Java Virtual Machine heap is the area of memory used by the JVM for dynamic memory allocation.
The heap is divided into generations:
1. Young Generation (Eden)–lived objects that are created and immediately garbage collected.
2. Old Generation(Tenured)- Objects that persist longer
3. Permanent Generation(PermGen)- class definitions and associated metadata
In order to configure the default sizes of heap parameter, we have some parameters which can be set at the time of starting up of application server or in the configuration file of it.
These parameters are :
-Xms : is the initial java heap size -Xmx : is the maximum java heap size -Xmn : the size of the heap for the young generation.
How does the heap size, settings and garbage collection affect?
Garbage collection runs primarily as 2 threads-
1. Lightweight– which performs small collections on the young generation of the heap
2. Full GC Thread – traverses the entire heap when there is not enough memory left to allocate space for objects which get promoted from the younger to the older generations
At initialization, a maximum address space is virtually reserved but not allocated to physical memory unless it is needed. The complete address space reserved for object memory can be divided into the young and tenured generations.
Objects are initially allocated in Eden and until they old enough to be tenured, or copied to the tenured generation.
If there is a memory leak or inadequate heap allocated, eventually the older generation will start to run out of room causing the Full GC thread to run more frequently and hence there can be pauses and it can become a performance issue.
So, how do we solve this problem?
The answer would be to customize the generation sizes.
Initial heap size: One major problem in large server application is the slow startup.This arises due to small initial heap size, so we need to give as much size as possible to virtual machine.
Size of generations: The amount allocated for the young generation is the value specified with -Xmn. The amount allocated for the older generation is the value of -Xmx minus the -Xmn. Generally, the young thread should not be too big or it will take too long for the GC to look through it for space that can be reclaimed.
So to decide the ratio of max and min need to check the verbose garbage collector output, and then explore the sensitivity of your individual performance metric to the garbage collector parameters.
So, the heart of a java application is JVM and if JVM and its constituents are fit and fine we can enjoy a healthy application.