JVM Tuning for improving performance

JVM Tuning

  1. set the heap size

    1. maximum and minimum

      1. -Xms2G -Xmx5G : minimum 2Gb and maximum 5Gb
    2. from java 8 heapsize can automatically increased

      1. -XX:MaxMetaspaceSize=<metaspace size>[unit] --> we are setting here the maximum heapsize
  2. Garbae Collection

    1. There are 4 GC implementation, which GC implementation we are using
  • Serial Garbage Collector

  • Parallel Garbage Collector

  • CMS Garbage Collector

  • G1 Garbage Collector

2.1

    -XX:+UseSerialGC
    -XX:+UseParallelGC
    -XX:+USeParNewGC
    -XX:+UseG1GC
  1. Monitor JVM GC performance : GC Logging

    1. to monitor application health we can check the jvm garbage collection perrformance by logging gc activity
-XX:+UseGCLogFileRotation 
-XX:NumberOfGCLogFiles=< number of log files > 
-XX:GCLogFileSize=< file size >[ unit ]
-Xloggc:/path/to/gc.log

For example, if we want to assign a maximum of 100 GC log files, each having a maximum size of 50 MB and want to store them in ‘/home/user/log/' location, we can use below syntax:

-XX:+UseGCLogFileRotation  
-XX:NumberOfGCLogFiles=10
-XX:GCLogFileSize=50M 
-Xloggc:/home/user/log/gc.log
  1. Handling out of memory

    1. jvm comes with paramters to dump heap memory into a physical file which can be used later for finding the memory leaks
-XX:+HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath=./java_pid<pid>.hprof
-XX:OnOutOfMemoryError="< cmd args >;< cmd args >" 
-XX:+UseGCOverheadLimit

5. Misc

  • -server: enables “Server Hotspot VM”; this parameter is used by default in 64 bit JVM

  • -XX:+UseStringDeduplication: Java 8u20 has introduced this JVM parameter for reducing the unnecessary use of memory by creating too many instances of the same String; this optimizes the heap memory by reducing duplicate String values to a single global char[] array

  • -XX:+UseLWPSynchronization: sets LWP (Light Weight Process) – based synchronization policy instead of thread-based synchronization

  • -XX:LargePageSizeInBytes: sets the large page size used for the Java heap; it takes the argument in GB/MB/KB; with larger page sizes we can make better use of virtual memory hardware resources; however, this may cause larger space sizes for the PermGen, which in turn can force to reduce the size of Java heap space

  • -XX:MaxHeapFreeRatio: sets the maximum percentage of heap free after GC to avoid shrinking.

  • -XX:MinHeapFreeRatio: sets the minimum percentage of heap free after GC to avoid expansion; to monitor the heap usage you can use VisualVM shipped with JDK.

  • -XX:SurvivorRatio: Ratio of eden/survivor space size – for example, -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden space to be 1:6,

  • -XX:+UseLargePages: use large page memory if it is supported by the system; please note that OpenJDK 7 tends to crash if using this JVM parameter

  • -XX:+UseStringCache: enables caching of commonly allocated strings available in the String pool

  • -XX:+UseCompressedStrings: use a byte[] type for String objects which can be represented in pure ASCII format

  • -XX:+OptimizeStringConcat: it optimizes String concatenation operations where possible