Heap size for JVM
Two JVM options are often used to tune JVM heap size:
-Xmx
for maximum heap size, and -Xms
for initial heap size. Here are some common mistakes made by developers while using them:- Missing
m, M, g
orG
at the end (they are case insensitive). For example,
The correct command should be:java -Xmx128 BigApp java.lang.OutOfMemoryError: Java heap space
java -Xmx128m BigApp
. To be precise,-Xmx128
is a valid setting for very small apps, like HelloWorld. But in real life, I guess you really mean-Xmx128m
- Extra space in JVM options, or incorrectly use =. For example,
The correct command should bejava -Xmx 128m BigApp Invalid maximum heap size: -Xmx Could not create the Java virtual machine. java -Xmx=512m HelloWorld Invalid maximum heap size: -Xmx=512m Could not create the Java virtual machine.
java -Xmx128m BigApp
, with no whitespace nor =. -X options are different than -Dkey=value system properties, where = is used.
- Only setting
-Xms
JVM option and its value is greater than the default maximum heap size, which is64m
. The default minimum heap size seems to be0
. For example,
The correct command should bejava -Xms128m BigApp Error occurred during initialization of VM Incompatible initial and maximum heap sizes specified
java -Xms128m -Xmx128m BigApp
. Its a good idea to set the minimum and maximum heap size to the same value. In any case, dont let the minimum heap size exceed the maximum heap size.
- Heap size is larger than your computers physical memory. For example,
The fix is to make it lower than the physical memory:java -Xmx2g BigApp Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
java -Xmx1g BigApp
- Incorrectly use
mb
as the unit, wherem
orM
should be used instead.java -Xms256mb -Xmx256mb BigApp Invalid initial heap size: -Xms256mb Could not create the Java virtual machine.
Comments
Post a Comment