In J2EE development, this is one of the common and frequent errors that developers may face. Sometimes it can be difficult to identify the actual cause, especially when the application runs out of JVM memory.
PermGen space or heap size (OutOfMemoryError) issues can be addressed by increasing the JVM memory allocated to Tomcat. The following are some approaches that can be used.
Solution 1: Set CATALINA_OPTS
Set the `CATALINA_OPTS` environment variable before starting Tomcat.
Linux / Unix — ksh / bash
export CATALINA_OPTS="-Xms512m -Xmx512m"
Linux / Unix — tcsh / csh
setenv CATALINA_OPTS "-Xms512m -Xmx512m"
Windows
set CATALINA_OPTS="-Xms512m -Xmx512m"
Stop the Tomcat server, set the `CATALINA_OPTS` environment variable, and then restart Tomcat.
You can check `tomcat-install/bin/catalina.sh` or `catalina.bat` to see how `CATALINA_OPTS` is used.
CATALINA_OPTS vs JAVA_OPTS
In `catalina.bat` or `catalina.sh`, you may notice that `CATALINA_OPTS`, `JAVA_OPTS`, or both can be used to specify JVM options.
The difference is that `CATALINA_OPTS` is intended specifically for Tomcat, whereas `JAVA_OPTS` can also be used for other Java applications.
I prefer to use `CATALINA_OPTS` when configuring options specifically for Tomcat, so that Tomcat does not unnecessarily pick up JVM options intended for other applications.
Solution 2: Change catalina.bat
Another option is to modify the `catalina.bat` file under the Tomcat `bin` directory.
Open:
tomcat-install/bin/catalina.bat
Search for:
CATALINA_OPTS, If `CATALINA_OPTS` is not already configured, you can set the required JVM options there.
For older Java versions, an example configuration was:
-Xms256m -Xmx512m -XX:MaxPermSize=256m
If this does not work, check the command used to start Java near the end of the `catalina.bat` file. Look for a line containing `%_EXECJAVA%` and `%JAVA_OPTS%`.
The JVM options can be added to that command. For example:
%_EXECJAVA% %JAVA_OPTS% -Xms256m -Xmx512m -XX:MaxPermSize=256m %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
In this example, `CATALINA_OPTS` has been removed from the command to avoid specifying the same JVM parameters more than once.
Important Note About MaxPermSize
`-XX:MaxPermSize` applies to older Java versions that used the PermGen memory area. Java 8 removed PermGen and replaced it with Metaspace.
Therefore, for Java 8 and later, do not use:
-XX:MaxPermSize=256m
For modern Java versions, the relevant options are typically `-Xms` and `-Xmx` for heap size, and `-XX:MaxMetaspaceSize` can be used when there is a specific need to limit Metaspace.
The exact JVM options should depend on the Java version being used by Tomcat.