<

JVM Usage tips and trick

Published on
15,817 Points
9,717 Views
1 Endorsement
Last Modified:
Approved
-Xmx and -Xms are the two JVM options often used to tune JVM heap size.

 

Here are some common mistakes made when using them:

 

Assume BigApp is a java class file for the below examples.



1.         Missing m, M, g or G at the end (they are case insensitive). For example,

java -Xmx128 BigApp
java.lang.OutOfMemoryError: Java heap space

The correct command should be “java -Xmx128m BigApp”.

 

2.         Extra space in JVM options, or incorrect use of =. For example,

java -Xmx 128m BigApp
Invalid maximum heap size: -Xmx
Could not create the Java virtual machine.

java -Xmx=512m BigApp
Invalid maximum heap size: -Xmx=512m
Could not create the Java virtual machine.

The correct command should be “java -Xmx128m BigApp”, with no whitespace nor =.

 

-X options are different than -Dkey=value system properties, where = is used.

 

3.         Only setting -Xms JVM option and its value is greater than the default maximum heap size, which is 64m. The default minimum heap size seems to be 0. For example,

java -Xms128m BigApp
Error occurred during initialization of VM
Incompatible initial and maximum heap sizes specified

The correct command should be “java -Xms128m -Xmx128m BigApp”.

 

It’s a good idea to set the minimum and maximum heap size to the same value. In any case, don’t let the minimum heap size exceed the maximum heap size.

 

4.         Heap size is larger than your computer’s physical memory. For example,

java -Xmx2g BigApp
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

The fix is to make it lower than the physical memory: java -Xmx1g BigApp



5.         Incorrectly use mb as the unit, where m or M should be used instead.

java -Xms256mb -Xmx256mb BigApp
Invalid initial heap size: -Xms256mb
Could not create the Java virtual machine.

 

Correct value should be “java -Xms256m -Xmx256m BigApp”  or “java -Xms256M -Xmx256M BigApp”

 

6.         The heap size is larger than JVM thinks you would ever need. For example,

java -Xmx256g BigApp
Invalid maximum heap size: -Xmx256g
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.

The fix is to lower it to a reasonable value “java -Xmx256m BigApp”

 

The value is not expressed in whole number. For example,

java -Xmx0.9g BigApp
Invalid maximum heap size: -Xmx0.9g
Could not create the Java virtual machine.


The correct command should be “java -Xmx928m BigApp”
1
Comment
Author:Rajeshpola
1 Comment
 

Expert Comment

by:mac_g
article is nice ...

please post more about  tunning tips too.

Thanks for article
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Join & Write a Comment

Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month