Could not reserve enough space for object heap
Could not create the Java virtual machine
What? Not enough RAM to run Java?
Having a VPS (virtual private server) means that you are sharing RAM with other VPS owners on the server. The actual hardware of the server may have 4GB RAM for example, however your VPS is only allocated a small portion of that RAM. The problem with running Java on a VPS lies within the VPS software and how it reports the available RAM to the software running inside of it. When you start Java on a VPS such as OpenVZ or Virtuozzo, Java sees the full available hardware RAM, not the small portion allocated to your VPS. It attempts to lock more RAM than it can and runs out of memory.
One solution to the problem is to ask your hosting company to increase the privvmpages amount in your VPS, however this request will most likely be rejected. Most hosting companies are not in business to give RAM away for free and they will suggest that you upgrade your VPS account.
How much RAM do you need to use Java? Well this of course depends on what your are running in Java. On a VPS, you are most likely running application servers such as Tomcat or JBoss. For Tomcat, you should allocate at least 128mb of RAM to the Java process. If you have a 256mb VPS, it just might work, however a 512mb VPS would be better. For JBoss, most people recommend at least 512mb of RAM. This of course leads you to need a 1gb RAM VPS.
If you are not sure how much RAM is currently in use, try this little script (that works on Virtuozzo and maybe others?) This will help you find out if your hosting company gave you what you are paying for.
echo “physpages = RAM in use (amount * 4096 = mb)”
echo “vmguarpages = RAM guarantee (allocated to VPS)”
echo “privvmpages = burst RAM”
Now back to the original problem, how to constrain Java to see the VPS RAM and not the entire server’s hardware. This is done using an export in the /etc/profile file.
#vi /etc/profile as root
#force java to limit self in this VPS
export _JAVA_OPTIONS=”-Xms20m -Xmx64m”
This export will give a default of 64mb to each Java process. You can override this default by using command line parameters to Java and also in your Tomcat or JBoss startup scripts.
There is another advantage to using the _JAVA_OPTIONS environment variable. Some files such as keytool and jar do not take memory settings at the command line. If you run keytool at the command line, it will most likely give you the Could not reserve enough space for object heap error. This can be automatically avoided by using the environment variable _JAVA_OPTIONS.