views:

192

answers:

2

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?

+4  A: 

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.

Jay Askren
And then tell us the answer! ;)
Adrian Lynch
There you go. Try it out and see if you get the same results I did.
Jay Askren
+1  A: 

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.

DigitalRoss
So what happens when you set it higher than RAM + Virtual memory?
TheLQ