By Shiraz Kanga-Oracle on Sep 30, 2014
Contributing Author: Shiraz Kanga, Consulting Member of Technical Staff, Oracle
Tuning Heap Size
- -Xms<n>[g|m|k] is the initial and minimum size of the Java heap
- -Xmx<n>[g|m|k] is the maximum possible size that the Java heap can grow upto
Figure 2. Heap after GC and Garbage Collection Overhead for a specific JVM
- The throughput of a JVM is measured in terms of the time spent doing garbage collection vs. the time spent outside of garbage collection (referred to as application time). It is the inverse of GC overhead mentioned above and represents the amount of work done by an application as a ratio of time spent in GC. Throughput can be tuned with -XX:GCTimeRatio=99 where 99 is the default which represents a 1% GC overhead.
- Latency is the amount of time delay that is caused by garbage collection. Latency for GC pauses can be tuned by specifying rhe maximum pause time goal with the command line option -XX:MaxGCPauseMillis=<N>. This is interpreted as a hint that pause times of <N> milliseconds or less are desired. By default, there is no maximum pause time goal. If a pause time goal is specified, the heap size and other garbage collection related parameters are adjusted in an attempt to keep garbage collection pauses shorter than the specified value. Note that these adjustments may cause the garbage collector to reduce the overall throughput of the application and in some cases the desired pause time goal cannot be met.
- -XX:PermSize=<n>[g|m|k] is the initial and minimum size of the permanent generation space.
- -XX:MaxPermSize=<n>[g|m|k] is the maximum size of the permanent generation space. If you ever get the message java.lang.OutOfMemoryError: PermGen space then it means that your application is loading a very large number of classes and this should be raised.
- -Xss=<n>[g|m|k]is the size of the thread stack. Each thread in a Java application has its own stack. The stack is used to hold return addresses, arguments to functions and method calls, and so on. The default stack size setting for a thread in Java is 1MB. In a highly multi-threaded system, like an application server at any given point in time there are multiple thread pools and threads that are in use so this may need to be reduced. Since stack size has to be allocated in contiguous blocks and if the machine is being used actively and there are many threads running in the system you may encounter an OutOfMemory error even when you have sufficient heap space. Recursive code can quickly exhaust the stack and if you use such code then you may need to increase the -Xss setting. However, if you see java.lang.OutOfMemoryError: unable to create new native thread then you may have too many threads, or each thread has a large stack; so you may need to decrease it.
Tuning Garbage Collection Algorithm
- Eden space: All objects are placed here when first created
- Survivor spaces: One or more regions where objects mature
- Tenured space: Where long lived objects are stored
- Permanent generation: This area is only used by the JVM itself to hold metadata, such as data structures for classes, methods, interned strings
- -XX:+UseSerialGC uses a single threaded, young generation, and old generation garbage collector (Normally this is a poor choice and should be used only for small Java heap sizes such as -Xmx256m or smaller)
- -XX:+UseParallelGC utilizes a multithreaded (parallel) garbage collector for the young generation and a single-threaded garbage collector for the old generation space in parallel.
- -XX:+UseParallelOldGC uses a multithread garbage collector for both the young and old generations.
- -XX:+UseParNewGC -> enables a multithreaded, young generation garbage collector
- -XX:+UseConcMarkSweepGC -> enables the VM’s mostly concurrent garbage collector. It also auto-enables -XX:+UseParNewGC (use if If you are not able to meet your application’s worst case latency requirements due to full garbage collection duration being too long)
- -XX:+UseG1GC -> garbage first collector (default in java 7, can be also used in latest releases of Java 6)
Java SE 6 HotSpot Virtual Machine Garbage Collection Tuning http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html