Child pages
  • Tuning the JVM
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Pre-requisites

Before conducting your tuning exercise:

  1. Define performance targets.
  2. Determine which version of the java JVM is available. More recent JVMs usually contain performance improvements of their own, especially in the area of garbage collection.
  3. Selection of a 64-bit JVM supports much more available memory.
  4. Select a garbage collector according to your needs and limitations. Bear in mind that garbage collectors have differing algorithms to suit particular goals and reduce the need to "stop the world". What is right for one implementation may not be right for another. You should test and compare with realistic scenarios and load in a pre-production environment.
  5. In tuning, start with the defaults and monitor the execution using jconsole and GC logs. Examine memory consumption and GC collection time and frequenscy and tune accordingly.

Caveat: These are recommendations only and it is recommended that customers refer to Oracle and other tuning documentation, and use a performance testing optimisation strategy to optimally configure IG according to deployment. Only an incremental optimisation strategy will determine the optimal configuration for a given deployment and user load.

Memory

As a proxy, IG is generally a low memory consumer, proxying request and response data between the client and server. Other factors to consider though in determining memory sizes are:

  • Caching (notification, session and request): size and lifetime
  • Resource size: as larger resources 

Heap Memory

  • IG largely consumes YoungGen space so JVM memory settings should be focussed there.
  • IG is a low user of OldGen memory, which remains largely static after startup, with a couple of exceptions to note:
    • if IG is proxying large resources then you may see more consumption here - as larger objects may be stored directly in OldGen memory.
    • Caching may increase the lifetime of objects, so obviating the the need for larger available OldGen space.

Non-heap Memory

  • Metaspace is the location where he JVM stores application metadata, for example class and method metadata. PermGen was renamed Metaspace in java 8, with some changes.
  • Be aware that while the Metaspace default size is unlimited, PermGen has a default maximum size (84Mb on a 64-bit JVM).

  • Note also that when the JVM resizes these spaces, a full GC must be done, which is expensive, so is worth monitoring usage during performance testing.

  • Incremental testing with anticipated load in a pre-production environment should easily identify the maximum required non-heap memory:
    • Setting this too low will result in OutOfMemoryErrors. Setting too high wastes memory.
    • Before java 8, inlined strings were also held in the PermGen space - adding a degree of variability that made determining the appropriate value difficult. This is not the case with later versions.

Memory Options

While configuring memory is a trial and error process, and best optimised incrementally, the following memory options are worth considering:

OptionDescription
-serverEnsures the JVM uses server-optimised configuration, compilation and execution. This is the default with a 64-bit JVM.

-XX:MetaspaceSize=<size>G

-XX:MaxMetaSpaceSize=<size>G

Configure initial and maximum Metaspace size (from java 8). 

or pre-java 8: 

-XX:PermSize=<size>G

-XX:MaxPermSize=<size>

Configure initial and maximum PermGen size (pre-java 8).

-XX:InitialHeapSize<size>G

-XX:MaximumHeapSize=<size>G

Configure the initial and maximum heap space:

  • Equivalent to -Xms and -Xmx flags respectively.
  • Oracle recommend that these figures are set the same to avoid expensive allocation operations. However, recent improvements in G1GC with its adaptive optimization algorithm (see below) means that this may limit vertical scaling. Though it is still recommended to follow this practice, if using G1GC, consider testing individual initial and maximum values too.
  • As a start point, IG has been shown to operate well in performance testing with a heapsize of 5Gb. That is, with this sized heap, even with more memory available, a memory increment was never done.

-XX:NewSize=<size>G

-XX:MaxNewSize=<size>G

Configure the initial and maximum YoungGen space:

  • This configuration is important to IG as a large consumer of Eden space.
  • Incrementally test to optimise, with anticipated concurrent load in a pre-production environment. The maximum should be within the overall allocated maximium heap-size (-Xmx), leaving space for non-heap memory.
  • If using G1GC, it is advised not to set these values and instead allow G1GC to optimise based on use. As ever, this should be tested.

-XX:+UseStringDeduplication

-XX:UseStringDeduplicationAgeThreshold=<N>

Prevent String duplication and conserve memory (java 8u20+). The threshold can be provided to specify the age after which a String becomes a candidate for deduplication.
-XX:MaxTenuringThreshold=<N>

Configure the number of transitions between survivor spaces before an object is moved to OldGen.

Because IG largely consumes YoungGen space, we can determine that anything that eventually would live in OldGen space could be moved there early to free up Eden space and reduce objects transitioning between survivor spaces. We could therefore set -MaxTenuringThreshold to a low value, possibly as low as 1 to accomplish this.

Garbage collection (GC)

Selecting the right garbage collector and tuning collection can reduce expensive "stop the world" pauses in collection. As IG is largely a consumer of YoungGen memory, there is a lot of scope for tuning to avoid expensive major collections (OldGen space consumption).

As mentioned, it is generally advisable to select from available, stable garbage collectors for the given JVM version according to your performance targets. Each garbage collector has its own goals and algorithms supporting that goal. For example, The Parallel GC aims to increase throughput whereas GCG1 aims to reduce latency. It is therefore advisable to test and compare. Note, the Concurrent Mark Sweep (CMS) collector has been omitted from discussion, having been deprecated in java 9.

Parallel GC

  • Default Hotspot collector in java 8 - in resource-rich environment (multiprocessor, memory availability).
  • Multiple threads for managing heap-space speeds up collection process.
  • Freezes application threads during GC.

The following Parallel GC options are worth considering:

OptionDescription
-XX:+UseParallelGCIndicate Parallel garbage collector should be used.
-XX:MaxGCPauseMillisSupports configuration of the target max time to pause in GC. This is purely a goal and not guaranteed. It does, however, allow some configuration between throughput (longer pauses) and latency (shorter pauses). Test values between 500ms - 2000ms.
-XX:GCPauseTimeIntervalSupports configuration of the ideal max time between pauses in GC. This is purely a goal and not guaranteed. It does, however, allow some configuration between throughput (longer pauses) and latency (shorter pauses). Test values between 500ms - 2000ms.
-XX:GCTimeRatioSupports configuration of the optimal ratio between time in GC and application time. This ratio is 1% by default and should not be configured to be greater than 5%.
-XX:ParallelGCThreads=<size> Number of threads to use in parallel operations

-XX:YoungGenerationSizeIncrement 

-XX:TenuredGenerationSizeIncrement

-XX:AdaptiveSizeDecrementScaleFactor

Adaptive sizing of each generation can be controlled using parameters using these parameters. The default increment is 20%. The -XX:AdaptiveSizeDecrementScaleFactor is the percentage to decrease on decrement, as a factor of the increment size. Default is 5%.

The following is an example set of JVM options using the Parallel GC (GC logging omitted):

JVM options with Parallel GC
-XX:InitialHeapSize=5g -XX:MaxHeapSize=5g \
-XX:NewSize=2G -XX:MaxNewSize=4G \
-XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M \
-XX:MaxTenuringThreshold=1 \
-XX:+UseStringDeduplication \
-XX:+UseParallelGC \
-XX:MaxGCPauseMillis=500

Garbage-First GC (G1GC) 

  • Available since java 7u4 and the default HotSpot collector since java 9 (in resource-rich environments).
  • A "Mostly Concurrent Collector", meaning a parallel, concurrent, and incrementally compacting low-pause collector.
  • Replaces the ConcurrentMarkSweep (CMS) collector (deprecated in java 9).
  • Concurrent collector, which conducts expensive operations concurrently with the application threads. 
  • Designed for multiprocessor environments with large available memory.
  • Designed to operate with good overall performance without the need to specify additional options.
  • Partitions heap into equal sized regions, each with a contiguous range of virtual memory
  • Minimises collection pauses in multiprocessor, memory-rich environments.
  • Uses an internal adaptive optimization algorithm to manage available heap-space, meaning it's adaptive to fast-memory growth/ spikes in load but also scales efficiently with lower load. With G1GC, this overrides -Xms  increments.

The following G1GC options are worth considering:

OptionDescription
-XX:+UseG1GCIndicate G1GC garbage collector should be used.
-XX:MaxGCPauseMillisSupports configuration of the target max time to pause in GC. This is purely a goal and not guaranteed. It does, however, allow some configuration between throughput (longer pauses) and latency (shorter pauses). Test values between 500ms - 2000ms.
-XX:GCPauseTimeIntervalSupports configuration of the ideal max time between pauses in GC. This is purely a goal and not guaranteed. It does, however, allow some configuration between throughput (longer pauses) and latency (shorter pauses). Test values between 500ms - 2000ms.
-XX:GCTimeRatioSupports configuration of the optimal ratio between time in GC and application time. This ratio is 1% by default and should not be configured to be greater than 5%.
-XX:ParallelGCThreads=<size> Number of threads to use in parallel operations.
-XX:ConcGCThreads=<size> Number of threads to use in concurrent operations - notably marking, which may reduce pauses.

-XX:G1NewSizePercent=<%>

-XX:G1MaxNewSizePercent=<%>

Supports managing the YoungGen space allocation as a percentage of the overall heap size. Ordinarily, this is best left unset to be managed by the G1GC adaptive optimisation algorithm, but could be used as a hint, given our understanding of IG's YoungGen usage.
-XX:G1HeapRegionSize Not recommended as it's an ergonomic setting based on heap size. It can be used, however, to manage the heap region size - to fine-tune how objects are allocated - which may be beneficial if large resources are being processed.

The following is an example set of JVM options using the G1GC (GC logging omitted):

Useful G1GC configuration options
-XX:InitialHeapSize=5g -XX:MaxHeapSize=5g \
-XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M \
-XX:MaxTenuringThreshold=1 \
-XX:+UseStringDeduplication \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=500\

References

  1. DZone: Choosing the Right GC
  2. Oracle Garbage Collection Tuning Guide
  3. Oracle G1GC Tuning Guide
  4. JVM Tuning with G1GC by @marknienaber on medium.com
  • No labels