Reputation: 775
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
Upvotes: 11
Views: 9553
Reputation: 108971
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
).
Upvotes: 8
Reputation: 476
On Linux, you can use the free
and awk
commands to calculate an inline value like this:
JAVA_OPT_MAX_MEM=$(free -m | awk '/Mem:/ {printf "-Xmx%dm", 0.80*$2}')
Example Result (on a machine with 3950m of free memory):
JAVA_OPT_MAX_MEM=-Xmx3160m
The calculated option is 80% of the total reported memory.
Upvotes: 1
Reputation: 43042
-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.
Upvotes: 27
Reputation: 115328
JVM is running on physical computer that has limited memory. Therefore JVM cannot have unlimited memory as far as the memory is limited by definition. You can however supply very huge limit in -Xmx
option.
The question is however why do you need the unlimited memory and even more correct question is how much memory do you really need?
Upvotes: 1