There is a “bug” or more correctly an error when someone is trying to run a simple Java Program inside a OpenVZ (HyperVM, Virtuozzo) VPS.

If you are in a harry and just want the answer, scroll down to the post and see it.

It the story goes like this:

 

The problem:

At first i run

 
[root@Mitsaras ~]# java -version
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

What that usually means is that you don’t have any available RAM to your system.

But i had plenty of RAM free. The host machine was mine. Before the error i had unlimited RAM, and them i limited it down to 256mb.

256MB might sound small, but for a simple CentOS minimal system, it plenty of RAM.

And indeed i had ~150MB free.

So that was not the error.

 

Trying to find the solution

I googled a little and came up with some suggestions which no one of them really worked.

The first one was (minimum, maximum heap size):

 
java -Xms16M -Xmx32M

What that does is limit the initial heap size to 16M (or something …). If you need more RAM than the default Virtual Machine value, this command usually works. But on my case it didn’t work.

** UPDATE: This actually works if you run all your programs with that command. But if at least one program is running without that command this will not fail. To set global limits for heap size scroll down to the end of the article)

Someone suggested that when java queries the system for the available free RAM the free RAM of the host is returned.

So java thinks that i have 8GB of free RAM and gets crazy. And also, java runtime at the beginning allocates huge amounts of memory, even though they will never be used. And the problem with that is that java need continues free space in the system.

But all those weren’t very helpfull.

 

PRIVVMPAGES – The real solution:

After some more googling i found the answer. So the answer is: you have to increase the number or PRIVVMPAGES of the VPS.

This can only be done by the administrator of the host machine that hosts your VPS.

The link explaining what PRIVVMPAGES is is this. So basically this controls how much memory some programm can allocate. Java asks for much memory, but this counter is usually low, so there is a problem.

If you are the owner of the host machine, or you want to know how to change that here is the way(s).

1st solution: [easier way following this one]

[root@TakisOSvistos ~]# vi /etc/vz/conf/103.conf

103 is the PEID of the VPS in my server 103.

Edit that file, and find the line:

 
# Auxiliary parameters
 
LOCKEDPAGES="9223372036854775807:9223372036854775807"
 
SHMPAGES="9223372036854775807:9223372036854775807"
 
PRIVVMPAGES="65536:65536"
 
NUMFILE="9223372036854775807:9223372036854775807"
 
NUMFLOCK="9223372036854775807:9223372036854775807"
 
NUMPTY="9223372036854775807:9223372036854775807"
 
NUMSIGINFO="9223372036854775807:9223372036854775807"
 
DCACHESIZE="9223372036854775807:9223372036854775807"

Look at like 5: PRIVVMPAGES=”65536:65536″

What i did was and an 1 in front of the value so that i see how it goes:

PRIVVMPAGES=”165536:165536″

Rebooted the VPS, and thats it, java running with no problem at all, even with 128mb RAM!

Second Way (No Reboot Required)

Type in the host OS command like:

 
vzctl set 103 --save --privvmpages 165000:165000

103 is the id of the VPS, 165000 is the limit of privvmpages

Another solution:

add

java -Xms25m -Xmx50m -Xss1024k

to all the programs you are running. This will save a lot of virtual memory (where in OpenVZ is just RAM!).

To set this globally, you must add something to the .bashrc file.

vi ~/.bashrc

and add:

export _JAVA_OPTIONS=-Xmx16m

That will take care whenever you manually start a java program. But if you run them with crons (like me) this won’t work. You have to add it to your cron file

crontab -e

and add the line:

_JAVA_OPTIONS="-Xmx16m"

If you have any questions or suggestions on how to make the posts or the Blog better, please write a comment, i really appreciate user feedback.

 

Thanks