Can the JVM max heap size be dynamic?

Viewed 17059

The JVM -Xmx argument lets one set the max heap size for the JVM to some value. But, is there a way to make that value dynamic? In other words, I want to tell the JVM "look, if you need it, just keep taking RAM from the system until the system is out."

Two-part reason for asking: First, the app in question can use a really wide range of ram depending on what the user is doing, so the conceptual min and max values are pretty far apart. Second, it would seem that the JVM reserves the max heap space from virtual memory at boot time. This particular app is run on a pretty wide variety of hardware, so picking a "one-size-fits-all" max heap space is hard since it has to be low enough to run on low-end hardware, but we'd really like to be able to take advantage of really beefy machines if they're available.

4 Answers

There is a JDK Enhancement Proposal (JEP) 8204088

"Dynamic Max Memory Limit"

that suggests to introduce CurrentMaxHeapSize:

To dynamically limit how large the committed memory (i.e. the heap size) can grow, a new dynamically user-defined variable is introduced: CurrentMaxHeapSize. This variable (defined in bytes) limits how large the heap can be expanded. It can be set at launch time and changed at runtime. Regardless of when it is defined, it must always have a value equal or below to MaxHeapSize (Xmx - the launch time option that limits how large the heap can grow). Unlike MaxHeapSize, CurrentMaxHeapSize, can be dynamically changed at runtime.

The expected usage is to setup the JVM with a very conservative Xmx value (which is shown to have a very small impact on memory footprint) and then control how large the heap is using the CurrentMaxHeapSize dynamic limit.

While there are no signs of this feature actively being worked at, it's relatively new JEP (from 2018), so I would still keep this in mind.

And company Jelastic (jelastic.com) has made a working prototype of JEP 8204088 for G1 garbage collector:

See description at http://mail.openjdk.java.net/pipermail/hotspot-gc-dev/2018-May/022077.html and list of patches for OpenJDK http://cr.openjdk.java.net/~tschatzl/jelastic/cmx/

Related