Asked in an interview. What happens if you specify max heap size (Xmx) greater than available RAM? I also wonder what happens if you specify min heap size (Xms) greater than available RAM?
3 Answers
The easiest way to find out is try it and see.
Edit: There are actually at least two answers to the question. Probably on a 64 bit system, as was mentioned, your app could grow and grow in memory usage and start thrashing. On a 32 bit system the story is a little different because the os is not able to give you that much heap space. For instance, if I run an app on Windows XP with 32 bit java with the command line option -Xmx2000m it will die with a message similar to the following:
Invalid maximum heap size: -Xmx2000m
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.
In Linux with 32 bit java, I get the following with -Xmx3000m:
Could not create the Java virtual machine.
Error occurred during initialization of VM
Could not reserve enough space for object heap
In Linux with 32 bit java, I get the following with -Xmx6000m
Invalid maximum heap size: -Xmx6000m
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.
Trying this with 64 bit Java, the JVM does allow you to allocate more memory than there is physical RAM, though if you ask for an extremely large amount of memory, the jvm will again fail with an error.

- 1
- 1

- 10,282
- 14
- 53
- 75
-
1And then tell us the answer! ;) – Adrian Lynch Sep 06 '10 at 00:54
-
There you go. Try it out and see if you get the same results I did. – Jay Askren Sep 06 '10 at 02:17
Only if your -Xms (minimum) is larger than available memory will you get an immediate failure on initialization of the JVM
$>java -Xms100g #JVM fails to start
Error occurred during initialization of VM Could not
reserve enough space for object heap
If your -Xmx (maximum) is larger than available memory your JVM does initialize since you are not using memory yet
$>java -Xmx100g #JVM starts up fine
Usage: java [-options] class [args...]
...
If your -Xmx (maximum) is larger than the available memory (total memory to include any virtual memory) you will get a runtime failure if and only if your JVM processes actually tries to use more memory than the machine has.
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00007f5feb100000, 927465472, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 927465472 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /some/file/path/hs_err_pid25.log
It wont 'thrash' until it nears your -Xmx limit, but if that limit is above your available memory you will get the above memory allocation error and your program will terminate before thrashing is even considered. (And that is very dramatic!)

- 839
- 9
- 15
-
Whether or not the system starts to swap heavily depends on your OS settings. For example, if you run Linux with unlimited memory overcommit enabled, the `mmap()` that JVM does to allocate RAM is going to succeed every time and the process will be killed if it uses more RAM than the system can provide. Note that you cannot safely use higher `-Mmx` setting than your RAM + swap can support because JVM has tendency to write all over the RAM it has allocated so memory overcommit with Java apps is not equally effective to native programs. – Mikko Rantalainen Sep 23 '22 at 08:25
-
Termination is dramatic, but it isn't necessarily worse than the alternative; trying to recover from OOME is not always a good idea according to https://stackoverflow.com/questions/3058198/can-the-jvm-recover-from-an-outofmemoryerror-without-a-restart @StevenWernerCS – Alexander Taylor Nov 04 '22 at 22:12
Nothing Dramatic
Although it can happen with certain low-end embedded systems, these days it would be quite rare to see a non-virtual Java environment even in embedded and impossible on a desktop or server.
So, nothing dramatic would happen, but once you use up available RAM, allocating additional (virtual) memory would just unnecessarily delay reclamation (garbage collection) and cause the program to start paging.
If severe, this condition is called "thrashing" and it is not a good thing. Stuff would run slowly.

- 143,651
- 25
- 248
- 329
-
1
-
For Linux with memory overcommit enabled, if you have `-Mmx` set to higher than RAM + swap, the system will start trashing if the Java process tries to use all of RAM and swap and if even more memory is *actually* used by the Java process, kernel "OOM killer" will kill the process if the only other option would be to crash the system. Note that "virtual memory" is different from swap space and 64 bit operating system always has nearly unlimited virtual memory for all practical purposes. Virtual memory is simply the whole address space and it may or may not be backed by RAM or swap. – Mikko Rantalainen Sep 23 '22 at 08:29