Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start a JVM with unlimited memory?

Tags:

java

memory

jvm

How do I start a JVM with no heap max memory restriction? So that it can take as much memory as it can ?

I searched if there is such an option, but I only seem to find the -Xmx and -Xms options.

EDIT:

Let's say I have a server with 4GB of RAM and it only runs my application. And, let's say I have another server with 32GB of RAM. I don't want to start my application with 4GB of memory limit because the second machine should be able to handle more objects

like image 220
Zoltan Ersek Avatar asked Sep 27 '16 06:09

Zoltan Ersek


2 Answers

-XX:MaxRAMFraction=1 will auto-configure the max heap size to 100% of your physical ram or the limits imposed by cgroups if UseCGroupMemoryLimitForHeap is set.

OpenJDK 10 will also support a percentage based option MaxRAMPercentage allowing more fine-grained selection JDK-8186248. This is important to leave some spare capacity for non-heap data structures to avoid swapping.

like image 75
the8472 Avatar answered Oct 03 '22 00:10

the8472


You can't (or at least, I don't know a JVM implementation that supports it). The JVM needs to know at start up how much memory it can allocate, to ensure that it can reserve a contiguous range in virtual memory. This allows - among others - for simpler reasoning in memory management.

If virtual memory would be expanded at runtime, this could lead to fragmented virtual memory ranges, making tracking and referencing memory harder.

However, recent Java versions have introduced options like -XX:MaxRAMPercentage=n, which allows you to specify the percentage of memory to allocate to the Java heap, instead of an absolute in bytes. For example -XX:MaxRAMPercentage=80 will allocate 80% of the available memory to the Java heap (the default is 25%).

The -XX:MaxRAMPercentage only works for systems with more than 200MB of memory (otherwise you need to use -XX:MinRAMPercentage, default 50%).

You can also use -XX:InitialRAMPercentage to specify the initial memory allocated to Java (MaxRAMPercentage is similar to -Xmx, InitialRAMPercentage is similar to -Xms).

like image 20
Mark Rotteveel Avatar answered Oct 03 '22 01:10

Mark Rotteveel