Friends of OpenJDK Today

Useful JVM Options: Heap Sizing

September 14, 2020

Author(s)

The HotSpot JVM has a lot of options available. Maybe too many. Sometimes we are looking for a specific option or the “magic” one that can give a serious boost in an application. Unfortunately, I think that magic option may not exist! However, some can help you for optimizing your application or for tuning some of its parts.

To find the complete list of options you will find in the globals.hpp file from OpenJDK sources. However, the VM Options Explorer, also integrated neatly here into foojay, can help you to navigate through the list.

I have summed up here, in my humble opinion, some of the most useful JVM options in the context of heap sizing.

Young Generation

Of course, you know the -Xms & -Xms options, which can also be abbreviated to -ms -mx, though did you know that parts of the Java heap and non-heap can also be sized:

  • -XX:NewSize=n Defines the initial size of the Young generation, including Eden, & Survivors.
  • -XX:MaxNewSize=n Defines the maximum size of the Young generation, including Eden & Survivors.
  • -XX:SurvivorRatio=n Ratio between Eden Size and one of the 2 survivors

n without unit is expressed in bytes, you can also use kKmMg & G to expresse respectively kilobytes, megabytes & gigabytes.

If NewSize < MaxNewSize, young generation size can be adjusted during application life. However, resizing does require a FullGC. To avoid this, set the same value for both options.

Metaspace

  • -XX:MetaspaceSize=n Defines the initial size of the Metaspace
  • -XX:MaxMetaspaceSize=n Defines the maximum size of the Metaspace generation

n without unit is expressed in bytes, you can also use kKmMg & G to express respectively kilobytes, megabytes & gigabytes.

If MetaspaceSize < MaxMetaspaceSize, Metaspace generation size can be adjusted during application life. However, resizing does require a FullGC. To avoid this, set the same value for both options.

Code Cache

  • -XX:InitialCodeCacheSize=n Defines the initial size of the Code Cache.
  • -XX:ReservedCodeCacheSize=n Defines the maximum size of the Code Cache.

n without unit is expressed in bytes, you can also use kKmMg & G to express respectively kilobytes, megabytes & gigabytes.

Code Cache stores the JITed code. This is an off-heap space, so GC does not reclaim it. If you reach the limit of the ReservedCodeCacheSize, the JIT compiler stops to compile more methods, since it cannot store them. So, if you have a lot of classes/methods need to be compiled, be aware of these options.

When you reach the limit, a warning is emitted on the standard output:

Java HotSpot(TM) Server VM warning: CodeCache is full. Compiler has been disabled"

with -XX:+PrintCompilation you will also get:

7383 COMPILE SKIPPED: code cache is full

Note: Used with permission and thanks — originally written and published by Jean-Philippe Bempel.

Author(s)

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard