Most of us who have worked with Apache-Tomcat in productions systems, would have faced out of memory errors sometimes or other.Â It is not that it always happens in tomcat but let us look at why does this occur and how to solve it.
Understanding Java Virtual Machine
A Java Virtual Machine on 32-bit operating systems typically has a maximum heap size of 64Mb. The JVM heap space is where all Java objects are stored, as well as memory used by the garbage collector. Sun recommends increasing this value for server applications.
Large server applications often experience two problems with these defaults. One is slow startup, because the initial heap is small and must be resized over many major collections. A more pressing problem is that the default maximum heap size is unreasonably small for most server applications.
64MBÂ is the heap size in tomcat as well by default and is too less for real world systems. When your application runs and you are able to analyze the app, this is what you would see
heap size in tomcatÂ
How to increase the heap size?
On windows :
Go to catalina.bat and set this
set JAVA_OPTS=-Xms128m -Xmx192m
On linux :
export CATALINA_OPTS="-Xms16m -Xmx256m";
-Xms â€“ is the minimum or initial size of your heap
-Xmx â€“ is the maximim size
export JAVA_OPTS="-Xms128m -Xmx256m";
On linux , you can also set in
setenv.shin the bin folder. The
catalina.sh script has logic to call into this script.
Note that CATALINA_OPTS is a better place than JAVA_OPTS. The former is only used when actually starting the Tomcat instance. JAVA_OPTS is also used to start JVMs that only interact with Tomcat (for example the JVM instance that is used to send the shutdown-message to a running Tomcat instance).
You can have a look at this article High Performance in Tomcat. Although this article is pretty old (dates back to Tomcat 4) , it still has lot of good points.
Once you have set the heap , how to check heap size ?
How to check heap size in linux
The output of
jmap -heap .