Categories
Java Technology

Running Java 6 on a Linux VPS

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.


cat /proc/user_beancounters
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.

5 replies on “Running Java 6 on a Linux VPS”

Thanks Europheus,

Your instructions helped running keytool though I still have Tomcat heap problems that I hope you can pass your eye over.

We have an Ubuntu server with 3.5 Gig memory. In it I have a VZContainer with 2 Gig. In that I’m running:
* MySql
* Confluence
* Jira
* Crowd (Atlassian also)

I fixed up the heap size as shown in your post using:

export CATALINA_OPTS=”-Xms128m -Xmx192m”

# I’m not sure which is being used so set both CATALINA_OPTS and JAVA_OPTIONS
export _JAVA_OPTIONS=”-Xms128m -Xmx192m”

and now top is showing (the user is ‘jira’ on server ‘jira’ – long story):


Mem: 2174896k total, 1716160k used, 458736k free, 0k buffers
Swap: 0k total, 0k used, 0k free, 0k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1833 jira 20 0 596m 175m 9092 S 1 8.3 0:47.99 java
1757 jira 20 0 474m 299m 9148 S 2 14.1 1:43.44 java
1728 jira 20 0 450m 118m 7348 S 9 5.6 0:24.24 java

But when I try to stop one of these (Crowd – PID: 1833) it fails with the heap error:


jira@jira:~$ /home/crowd/apache-tomcat/bin/shutdown.sh
Using CATALINA_BASE: /home/crowd/apache-tomcat
Using CATALINA_HOME: /home/crowd/apache-tomcat
Using CATALINA_TMPDIR: /home/crowd/apache-tomcat/temp
Using JRE_HOME: /usr/lib/jvm/java-6-sun
Picked up _JAVA_OPTIONS: -Xms128m -Xmx192m
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

Top tells me that there is 458736k (447Meg) free, and I’ve said to grab a max of 192 meg for a JVM. So why is it still giving this error?

Many thanks for any feedback.

Cheers,

Brooke

Brooke, try the MaxPermSize option in the JVM parametrs, such as -XX:MaxPermSize=256m and see if that helps.

Thanks Europheus,

I’ve tried to post a comment multiple times over days now. If it fails (missing field + doesn’t seem to like anchors in it) then I can’t correct and repost as it says “already posted that”. WordPress is causing problems by the looks of things.

Your suggestion worked, thanks.

Cheers,

Brooke

Brooke, glad to hear your Java problem is resolved! And thanks for letting me know about the WordPress issues. The anti-spam plugin was broken and I replaced it with a new one, which hopefully works better.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.