Keywords: Tomcat | Memory Configuration | PermGen Space
Abstract: This article provides an in-depth analysis of PermGen space memory overflow issues encountered when running Java web applications on Apache Tomcat servers. By examining the permanent generation mechanism in the JVM memory model and presenting specific configuration cases, it systematically explains how to correctly set heap memory, new generation, and permanent generation parameters in catalina.sh or setenv.sh files. The article includes complete configuration examples and best practice recommendations to help developers optimize Tomcat performance in resource-constrained environments and avoid common OutOfMemoryError exceptions.
Problem Background and Analysis
During Java web application deployment, Apache Tomcat servers frequently face memory management challenges. According to user reports, even with sufficient heap memory configuration (e.g., -Xms1024m -Xmx3052m), "java.lang.OutOfMemoryError: PermGen space" errors persist when running multiple large applications like Artifactory and Jenkins simultaneously. This phenomenon highlights the limitations of adjusting only heap memory size.
In-depth JVM Memory Model Analysis
The Java Virtual Machine memory is divided into multiple regions, with the Permanent Generation responsible for storing class metadata, method areas, and constant pools. Unlike heap memory, permanent generation size requires separate configuration. In long-running web servers, frequent class loading and unloading operations can easily exhaust permanent generation space, particularly when deploying multiple large applications.
Configuration Solution
To address permanent generation space insufficiency, we recommend creating the following configuration in the <code>${TOMCAT_HOME}/bin/setenv.sh</code> file:
CATALINA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8
-server -Xms1536m -Xmx1536m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"This configuration implements a balanced memory allocation strategy: setting initial and maximum heap to 1536MB ensures stable application operation; 256MB new generation optimizes garbage collection efficiency; permanent generation initial and maximum values of 256MB prevent metadata overflow.
Configuration Parameter Details
-Xms and -Xmx: Control the initial and maximum size of JVM heap memory respectively. Setting identical values avoids performance fluctuations caused by runtime memory adjustments.
-XX:PermSize and -XX:MaxPermSize: Critical parameters defining the initial size and upper limit of permanent generation space. For multi-application deployment environments, 256MB or higher is recommended.
-XX:NewSize and -XX:MaxNewSize: New generation configuration affecting young generation garbage collection frequency. Appropriate increases can reduce Full GC occurrences.
-XX:+DisableExplicitGC: Disables explicit System.gc() calls, avoiding unnecessary performance overhead.
Implementation Steps and Verification
First, check the bin folder of the Tomcat installation directory. If setenv.sh doesn't exist, create a new file. After pasting and saving the configuration code, grant execution permission using <code>chmod +x setenv.sh</code>. Restart the Tomcat service and monitor memory usage with the <code>jstat -gc <pid></code> command to confirm permanent generation space remains stable within configured limits.
Best Practice Recommendations
Dynamically adjust parameters based on server physical memory capacity: in 8GB memory environments, allocate 2-4GB for heap memory and 256-512MB for permanent generation. Regularly monitor GC logs and use tools like VisualVM to analyze memory leaks. For production environments, determine optimal configurations through stress testing.
Conclusion
Tomcat memory optimization requires comprehensive consideration of heap memory, new generation, and permanent generation coordination. Through the solutions provided in this article, developers can effectively resolve PermGen space overflow issues and enhance stability and performance in multi-application deployments. Proper memory management strategies form the foundation for efficient Java web application operation.