Example: Production Tomcat (Java 8)
Add to setenv.sh:
-Xms8g -Xmx8g -XX:+UseG1GC
⚠️ JVM tuning does not fix leaks, only delays failure.
jcmd <PID> GC.run
Check memory release:
jmap -histo <PID>
✔ Code review for static leaks
✔ Mandatory resource closing
✔ Regular heap dump analysis
✔ Scheduled Tomcat restart (only if unavoidable)
✔ Monitoring via Prometheus / JMX
| Area | Action |
|---|---|
| Root cause | Application code |
| Detection | Heap dump + GC logs |
| Fix | Code cleanup |
| Prevention | Monitoring & best practices |
If you want, I can provide:
Tomcat memory leak SOP
Heap dump analysis checklist
Production JVM tuning template
Just let me know.
Below are the commonly used and recommended Tomcat (JVM) memory parameters, with clear explanations and production-ready examples.
-Xms4g # Initial heap size
-Xmx4g # Maximum heap size
Best practice: Keep -Xms = -Xmx for stable performance.
-XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m
Controls class metadata memory (important for Tomcat apps).
-Xss512k
Too many threads + large stack = OutOfMemoryError.
-XX:+UseG1GC
Optional tuning:
-XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=45
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/tomcat/heapdump -XX:+ExitOnOutOfMemoryError
-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/opt/tomcat/logs/gc.log
Add to setenv.sh:
export CATALINA_OPTS="
-Xms8g
-Xmx8g
-XX:MetaspaceSize=256m
-XX:MaxMetaspaceSize=512m
-Xss512k
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/opt/tomcat/heapdump
"
jmap -heap <PID>
jstat -gc <PID> 5s
| Server RAM | Tomcat Heap |
|---|---|
| 8 GB | 4–5 GB |
| 16 GB | 8–10 GB |
| 32 GB | 16–20 GB |
Leave memory for:
OS
File cache
Other services
❌ Setting heap larger than system RAM
❌ Ignoring Metaspace
❌ Relying only on JVM tuning for leaks
Add to setenv.sh:
export CATALINA_OPTS="
-Xms8g
-Xmx8g
-XX:MetaspaceSize=256m
-XX:MaxMetaspaceSize=512m
-Xss512k
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/opt/tomcat/heapdump
Before we start, make sure you have Apache Tomcat installed on your server or computer. If you haven’t installed it yet, you can download it from the official Tomcat website and follow the installation instructions.
<tomcat-users> tags to create a user with the manager role:
<role rolename="manager-gui"/>
<user username="admin" password="admin_password" roles="manager-gui"/>
Replace admin with your desired username and admin_password with a strong password.
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192\.168\.1\.\d+" />
This example allows access from any IP address in the 192.168.1.x range.
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192\.168\.1\.\d+" />
This example allows access from any IP address in the 192.168.1.x range.
For the changes to take effect, you need to restart Tomcat. You can do this by running the shutdown script followed by the startup script located in the bin directory of your Tomcat installation:
shutdown.bat
startup.bat
./shutdown.sh
./startup.sh
Open a web browser and go to http://<your-server-ip>:8080/manager. You should see the Tomcat Manager login page. Enter the username and password you created in the tomcat-users.xml file.
======================
file edit
setenv.sh
export CATALINA_OPTS="$CATALINA_OPTS -Xms28672m"
export CATALINA_OPTS="$CATALINA_OPTS -Xmx28672m"
======================