Using Tomcat (or anything Java) on a virtual server

Using Tomcat (or anything Java) on a virtual server

When it comes to serving web pages, two technical trends (among others) are prevalent: virtual servers and server side Java. To the end user, both are completely transparent, and even the server administrator might forget the fact that his server is virtual, because from experience he can tell that it behaves exactly as a physical server. It seems only natural (not even worth mentioning) to combine both trends.
In fact, this is not as easy as you might guess. Lets say you have a virtual server, running Ubuntu 9.04, and have installed sun-java6-jdk, and decompressed tomcat-6.0.18 somewhere.  The most trivial way to start it is

tomcat-6.0.18/bin/startup.sh

which is likely to tell you something like

Using CATALINA_BASE: /(...)/tomcat-6.0.18
Using CATALINA_HOME:   /(...)/tomcat-6.0.18
Using CATALINA_TMPDIR: /(...)/tomcat-6.0.18/temp
Using JRE_HOME:       /usr/lib/jvm/java-1.6.0-sun

While this might seem as if the server was started successfully, nothing really happened. A look into /(…)/tomcat-6.0.18/logs/catalina.out might show something like

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

This is due to the way memory management is handled on virtual servers, which you can read here. It has nothing to with the actual amount of RAM that Tomcat needs, it also appears that a simple command like „java -version“ tries to allocate more memory then it is allowed to (284 MB on my vServer). Several sites recommend to set the -Xmx option when starting Java, mostly by modifying scripts which are not made to be modified. Some others advise changing /etc/java-1.5.0-sun/jvm.cfg to prefer the client VM over the server VM, or even to delete the server VM and symlink it to the client version.
Not only is it highly absurd that you have to use a client VM on a server just because the server is not capable of running the server VM, but it also is impossible, because Java 6 seams to have no client implementation on 64 bit processors – and of course virtual servers run on 64 bit machines (mine has at least 44 GB of RAM, how would it handle this on 32 bit?).
Using -Xmx seems to work, even though you do not want to change each and every script that calls java or javac. To encompass this, you can set the environment variable JAVA_OPTS to something like JAVA_OPTS=“-Xms10m -Xmx256m“, which is definitely picked up by Tomcat, and maybe used by other scripts which run java or javac. Sadly, many scripts do not, and there seems to be not way to make java „just run“ on a virtual server.
With the proper options, java does run, but Tomcat does not. It keeps on saying

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

I started writing this blog post hoping that I would conclude with a working solution, but as it seems right now, I just cannot find one. I will keep trying, possibly not only searching the web but also posting questions like I did on serverfault.com and in the end I will update this post to reflect the solution.
One more note: whil this works for calls to the „java“ binary, it does not for „javac“, which does not directly accept -Xms and -Xmx. You have to use -J-Xms and -J-Xmx instead.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.