Search This Blog

Showing posts with label profile. Show all posts
Showing posts with label profile. Show all posts

Tuning Garbage Collection with Oracle JDK


When using Oracle's JDK, the goal in tuning garbage collection performance is to reduce the time required to perform a full garbage collection cycle. You should not attempt to tune the JVM to minimize the frequency of full garbage collections, because this generally results in an eventual forced garbage collection cycle that may take up to several full seconds to complete.
The simplest and most reliable way to achieve short garbage collection times over the lifetime of a production server is to use a fixed heap size with the collector and the parallel young generation collector, restricting the new generation size to at most one third of the overall heap.
Oracle recommends using the Garbage-First (G1) garbage collector. See "Getting Started with the G1 Garbage Collector" for more information on using the Garbage-First collector.
The following example JVM settings are recommended for most production engine tier servers:
-server -Xms24G -Xmx24G -XX:PermSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=20 -XX:ConcGCThreads=5 -XX:InitiatingHeapOccupancyPercent=70
For production replica servers, use the example settings:
-server -Xms4G -Xmx4G -XX:PermSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=20 -XX:ConcGCThreads=5 -XX:InitiatingHeapOccupancyPercent=70
For standalone installations, use the example settings:
-server -Xms32G -Xmx32G -XX:PermSize=512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=20 -XX:ConcGCThreads=5 -XX:InitiatingHeapOccupancyPercent=70
The above options have the following effect:
  • -Xms, -Xmx: Places boundaries on the heap size to increase the predictability of garbage collection. The heap size is limited in replica servers so that even Full GCs do not trigger SIP retransmissions. -Xms sets the starting size to prevent pauses caused by heap expansion.
  • -XX:+UseG1GC: Use the Garbage First (G1) Collector.
  • -XX:MaxGCPauseMillis: Sets a target for the maximum GC pause time. This is a soft goal, and the JVM will make its best effort to achieve it.
  • -XX:ParallelGCThreads: Sets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running.
  • -XX:ConcGCThreads: Number of threads concurrent garbage collectors will use. The default value varies with the platform on which the JVM is running.
  • -XX:InitiatingHeapOccupancyPercent: Percentage of the (entire) heap occupancy to start a concurrent GC cycle. GCs that trigger a concurrent GC cycle based on the occupancy of the entire heap and not just one of the generations, including G1, use this option. A value of 0 denotes 'do constant GC cycles'. The default value is 45.

see also

Diagnose Java OutOfMemoryError

Diagnose

  • add -XX:+HeapDumpOnOutOfMemoryError to dump Java heap
  • use jmap while the process is runnin:
    • jmap -histo:live pid
    • jmap -dump pid
  • use jhat or Eclipse MAT to analyse the heap dump file

Solutions

Java: UseG1GC to reduce memory consumption

Add java arugments:
-XX:+UseG1GC -XX:+UseStringDeduplication



Note: UseStringDeduplication only works with UseG1GC. From my experience, it does reduce the memory consumption significantly for some applications. But it also slows down the process a little bit compared with the default Parallel GC.


see also

Java: jmap heap dump

To capture a heap dump using jmap we need to use the dump option:
jmap -dump:[live],format=b,file=<file-path> <pid>

Along with that option, we should specify several parameters:
  • live: if set it only prints objects which have active references and discards the ones that are ready to be garbage collected. This parameter is optional
  • format=b: specifies that the dump file will be in binary format. If not set the result is the same
  • file: the file where the dump will be written to
  • pid: id of the Java process

An example would be like this:
jmap -dump:live,format=b,file=/tmp/dump.hprof 12587


see also

Analyse Java Heap Dump with Eclipse Memory Analyser Tool








see also

Dump Java Heap when OutOfMemoryError occurs

  • Add argument
    -XX:+HeapDumpOnOutOfMemoryError
    and it will dump to java_pid.hprof file in the working directory.
  • Add argument
    -XX:HeapDumpPath=/path/to/output-dir
    to specify the output directory for the dump file. For example:
    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$HOME/JavaHeapDumps/






see also