02 Oct 2020
Support Center
»
Knowledgebase
»
Java's -Xmx -Xms Heap Settings
Java's -Xmx -Xms Heap Settings
Solution
Java's -Xmx Heap Settings
Java has a couple of settings that help control how much memory it uses:
If you get errors like :-
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
You will need to use the below parameters :-
-Xmx sets the maximum memory heap size
-Xms sets the minimum memory heap size.
Keep -Xms Small
For a server with a 'small' amount of memory, then we recommend that -Xms is kept as small as possible. e.g. -Xms 16m. Some customers set this higher, but that can lead to issues. e.g. the command that restarts tomcat runs a java process. That Java process picks up the same -Xms setting as the actual Tomcat process. So you will effectively be using two times -Xms when doing a restart. If you set -Xms too high, then you may run out of memory.
Don't Let -Xmx Be Too Low
When setting the -Xmx setting you should consider a few things... -Xmx has to be enough for you to run your app. If it is set too low then you may get Java OutOfMemory exceptions (even when there is sufficient spare memory on the server).
Figuring Out a Reasonable -Xmx
If you have 'spare' memory, then increasing the -Xmx setting is often a good idea. Just note that the more you allocate to Java the less will be available to your database server or other applications and less for Linux to cache your disk reads.
Whatever you do, you should ensure that the total of the -Xmx settings does not exceed the 'spare' memory on your server. You can figure your 'spare' memory by stopping your Java processes and starting up everything else that is normally running on your server. When you run free -m it will tell you how much memory you are using. If you subtract that value from your total memory and then subtract a bit more (for safety) that will be the maximum you should allocate with -Xmx. e.g. if you have 96MB 'spare' then you could allocate two -Xmx48m Java processes. Or one -Xmx96m process.
Tomcat -Xmx Settings
We put our -Xmx/-Xms settings for Tomcat in /usr/local/tomcat/bin/setenv.sh in the JAVA_OPTS variable. We find that -Xmx48 is sufficient to run a minimal Tomcat with the sample webapps installed.
Heads Up If You Have a Big/Complex App
Note that Java can end up using (a lot) more than the -Xmx value worth of memory, since it allocates extra/separate memory for the Java classes it uses. So the more classes are involved in your application the more memory that Java process will require.
Resolving java.lang.OutOfMemoryError: PermGen
If you get an error like:
java.lang.OutOfMemoryError: PermGenThe PermGen space is used for things that do not change (or change often). e.g. Java classes. So often large, complex apps will need lots of PermGen space. Similarly if you are doing frequent war/ear/jar deployments to running servers like Tomcat or JBoss you may need to issue a server restart after a few deploys or increase your PermGen space.
To increase the PermGen space use something like: -XX:MaxPermSize=128m
The default is 64MB.
(Note that Xmx is separate from the PermGen space, so increasing Xmx will not help with the PermGen errors).
The PermGen memory in addition to the Xmx memory. e.g. -Xmx128m and MaxPermSize=128m would use up to 256MB.
Article Details
Article ID:
17
Created On:
07 Apr 2020 09:15 AM
This answer was helpful
This answer was not helpful
User Comments
Add a Comment
Sharing is good. So if you have a comment about this entry please feel free to share. The Comments might be reviewed by our Staff and might require approval before being posted. Questions posted will not be answered, please submit a ticket for support requests.
Image Verification Required
Please enter the characters that appear to the right in the space provided. This is just to verify that you are a human.
Fullname:
Email: (Optional)
Comments:
Back
Login
Username:
Password:
Remember Me:
Search
-- Entire Support Site --
Knowledgebase
Downloads
Troubleshooter
Article Options
Add Comment
Print Article
PDF Version
Email Article
Add to Favorites
Home
|
Knowledgebase
|
Troubleshooter
|
News
|
Downloads
Language:
English (U.S.)