According to this link, there is an option to set MaxRamSize manually to restrict the JVM to not use memory beyond this. But I have not seen any documentation of the same. I
Why I'm looking for this kind of an option to run the application inside a container (Like Docker) and prevent the application from being killed by the OOM Killer.
This is exactly the option you are looking for. Moreover, it was properly integrated with cgroups
(~docker) as part of JDK-8170888.
Note that this flag does not prevent JVM from allocating more memory than its value, but rather gives a hint "I know my physical RAM limit is X, please allocate within X", so JVM will share this value between Java heap and native memory. But if e.g. your application has native memory leak or classloader leak, then this limit will be reached anyway.
JVM is complaining on MaxRAM=1073741824B
because it doesn't expect B
in the end, it should be declared as -XX:MaxRAM=1073741824
.
See the description of the flag: Real memory size (in bytes) used to set maximum heap size
.
I think java
is complaining about the "B". According to the manual entry, the java
command understands k
/ K
or m
/ M
or g
/ G
suffixes for sizes. It doesn't mention "B".
(Did you notice that the error message says "improperly specified", not "unknown"? That hints that the java
command has recognized the option, but the syntax is incorrect. See above ...)
Looking at the OpenJDK source code for Java 11, I can see a MaxRAM
parameter defined in the "gc_globals.hpp" file.
I found the reference
https://chriswhocodes.com/hotspot_options_jdk11.html?s=MaxRam
But as Stephen C suggests it is probably just the B
NOTE: This is the maximum RAM size used to calculate the default Heap and Direct memory maximum. It doesn't provide a greater enforcement.
Real memory size (in bytes) used to set maximum heap size
and for another parameter
Maximum ergonomically set heap size (in bytes); [the default] zero means use MaxRAM * MaxRAMPercentage / 100
In the Oracle/OpenJDK there is no obvious option.
$ java -version -XX:+PrintFlagsFinal | grep -i MaxRAM
However, on Linux you can set the maximum memory size with ulimit
however this will hard crash the JVM if reached.