How to check the current and maximum PermGen size of Tomcat

Checking current JVM memory limits

During development, when redeploys occur frequently, you might run into this error from time to time: java.lang.OutOfMemoryError: PermGen space. To get an overview of JVM memory organization, read this short article. In addition, here is a quote from a detailed SUN paper on JVM memory management:

PermGen space [...] is the area of the heap where the JVM stores its metadata. If an application loads a large number of classes, then the permanent generation may need to be increased. You can do so by specifying the command line option `–XX:MaxPermSize=n`, where `n` specifies the size.

Use the following commands to check Tomcat’s current PermSize or MaxPermSize values on debian:

sudo -u <tomcat-user> jinfo -flag PermSize <tomcat-process-pid>
sudo -u <tomcat-user> jinfo -flag MaxPermSize <tomcat-process-pid>

Real life example:

:~# sudo -u tomcat jinfo -flag PermSize 8340
-XX:PermSize=21757952

The sudo -u tomcat command is necessary to avoid the following error message: Unable to open socket file: target process not responding or HotSpot VM not loaded. You can use ps auxc to look up ids of running processes and ps auxc | grep java to show results containing the word java only:

:~# ps auxc | grep java
tomcat    8340  0.5  9.2 945068 387276 ?       Sl   17:04   0:50 java

Here, the process id is 8340. You can also use the server status page of Tomcat’s manager application at http://domain.com:8080/manager/status/all to view initial, total and maximum values for various memory settings.

Increasing JVM memory limits

If you want to increase the PermGen and maximum heap size, create a file called setenv.sh in $CATALINA_HOME/bin/ (see here for Windows instructions) and place the following in it:

export JAVA_OPTS="-Xmx1024m -XX:MaxPermSize=128m"

Stop and restart Tomcat afterwards. Using ps aux you can see that the option has been picked up:

:~# ps aux | grep java
tomcat    9419 11.2 11.5 1514856 482976 ?      Sl   23:52   0:27 /usr/lib/jvm/java-7-oracle/bin/java -Djava.util.logging.config.file=/opt/tomcat//conf/logging.properties -javaagent:/opt/tomcat//lib/openejb-javaagent.jar <em>-XX:MaxPermSize=128m</em> -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/tomcat//endorsed -classpath /opt/tomcat//bin/bootstrap.jar:/opt/tomcat//bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat/ -Dcatalina.home=/opt/tomcat/ -Djava.io.tmpdir=/opt/tomcat//temp org.apache.catalina.startup.Bootstrap start

Let’s see what jinfo tells us:

:~# sudo -u tomcat jinfo -flag MaxPermSize 9419
-XX:MaxPermSize=134217728

Where 134217728 byte = 128 * 1024 * 1024 byte = 128 MiB.