Jvm configuration
Explicit Heap Memory – Xms and Xmx Options
One of the most common performance-related practices is to initialize the heap memory as per the application requirements.
That’s why we should specify minimal and maximal heap size. Below parameters can be used for achieving it:
-Xms<heap size>[unit]
-Xmx<heap size>[unit]
Here, unit denotes the unit in which the memory (indicated by heap size) is to be initialized. Units can be marked as ‘g’ for GB, ‘m’ for MB and ‘k’ for KB.
For example, if we want to assign minimum 2 GB and maximum 5 GB to JVM, we need to write: -Xms2G -Xmx5G
Garbage Collection
JVM has four types of GC implementations:
- Serial Garbage Collector
- Parallel Garbage Collector
- CMS Garbage Collector
- G1 Garbage Collector
These implementations can be declared with the below parameters:
-XX:+UseSerialGC
-XX:+UseParallelGC
-XX:+USeParNewGC
-XX:+UseG1GC
Handling out of Memory
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./java_pid<pid>.hprof
-XX:OnOutOfMemoryError=”< cmd args >;< cmd args >”
-XX:+UseGCOverheadLimit
- HeapDumpOnOutOfMemoryError instructs the JVM to dump heap into physical file in case of OutOfMemoryError
- HeapDumpPath denotes the path where the file is to be written; any filename can be given; however, if JVM finds a <pid> tag in the name, the process id of the current process causing the out of memory error will be appended to the file name with .hprof format
- OnOutOfMemoryError is used to issue emergency commands to be executed in case of out of memory error; proper command should be used in the space of cmd args. For example, if we want to restart the server as soon as out of memory occur, we can set the parameter: -XX:OnOutOfMemoryError=”shutdown -r”
- UseGCOverheadLimit is a policy that limits the proportion of the VM’s time that is spent in GC before an OutOfMemory error is thrown