On this page
Short answer
Tomcat's Metaspace grows after every redeploy when something still references the old web application class loader, so the old classes stay loaded. The JDK does not cap Metaspace by default, so the growth ends in java.lang.OutOfMemoryError: Metaspace only if you set a cap, and otherwise in a container kill. Compare class loaders before and after a redeploy with jcmd, then find the reference holding the old one.
Tomcat's Metaspace grows after every redeploy when something still references the old web application class loader, so the old classes stay loaded. The JDK does not cap Metaspace by default, so the growth ends in java.lang.OutOfMemoryError: Metaspace only if you set a cap, and otherwise in a container kill. Compare class loaders before and after a redeploy with jcmd, then find the reference holding the old one.
If redeploys are part of your release routine, our Tomcat consulting engineers use the same sequence below. The ThreadLocal and RMI defaults come from the Tomcat 10.1 Context page, and the memory settings come from the JDK 21 manual.
Why does Metaspace grow after a Tomcat redeploy?
Tomcat gives each web application its own class loader, and a reload or redeploy creates a new one. The old loader can only be garbage collected when nothing references it. A single leftover reference keeps every class the old application loaded, so the Metaspace size climbs by roughly one application per redeploy or undeploy cycle.
The JDK does not limit Metaspace by default: -XX:MaxMetaspaceSize is unlimited unless you set it, and -XX:MetaspaceSize is only the threshold for the first garbage collection, not a cap. So a leak grows quietly until the host or container runs out of memory.
What usually holds the old class loader?
The same few patterns are the usual cause of a redeploy leak and none of them raises an error until memory runs out. The table lists them with what Tomcat does about each, per its documentation.
| Holder | What Tomcat does by default | Your fix |
|---|---|---|
| A ThreadLocal on a pooled thread, holding an application class | clearReferencesThreadLocals is true and clears them | Remove the value in a finally block |
| A JDBC driver registered in DriverManager from WEB-INF/lib | Tries to deregister drivers it finds; expects the application to do it | Deregister in a ServletContextListener, or put the driver in the shared lib directory |
| A thread or Timer started by the application | clearReferencesStopThreads and clearReferencesStopTimerThreads are false | Stop it in contextDestroyed |
| An RMI target | clearReferencesRmiTargets is true but needs a JVM export option | Remove the registration on shutdown |
| A keep-alive thread from the JDK HTTP client | clearReferencesHttpClientKeepAliveThread is true and moves its loader | Usually nothing |
The Tomcat docs call clearReferencesStopThreads a last resort for development, not production, because it uses Thread.stop().
How do I confirm a leak with jcmd?
Take a baseline of Metaspace by class loader, redeploy, then measure again. Two loaders for one application, where only one should exist, mean the old one is leaking.
- Run
jcmd <pid> VM.metaspace show-loadersand save the output. Theshow-loadersoption reports usage by class loader. - Redeploy the application once, and trigger a garbage collection through your normal tooling.
- Run the same command again and compare the web application loaders.
- Run
jcmd <pid> GC.class_histogrambefore and after. By default it counts only reachable objects, and its impact is rated high on a large heap, so use a quiet window. - If an old loader remains, write a heap dump with
jcmd <pid> GC.heap_dump /path/dump.hprofand trace what references it in a heap analyser.
Tomcat's Manager also has a findleaks command. The documentation warns that it triggers a full garbage collection, to use it with extreme caution on production systems, and to confirm its results with a profiler.
Which settings stop the next one?
- Keep the JDBC driver in the shared
libdirectory. The Tomcat datasource guide says to copy the driver jar to$CATALINA_HOME/lib, not intoWEB-INF/lib. - Leave
JreMemoryLeakPreventionListenerinserver.xml. ItsdriverManagerProtectionandurlCacheProtectionare true by default, and the second one disables URL connection caching. - Set
-XX:MaxMetaspaceSizefrom a measured baseline, so a leak fails early with a clear message. - Prefer a restart over a hot redeploy for releases, and run it behind a load balancer. Our Tomcat in production guide covers the rest of the production defaults.
Redeploy leaks live in application code, so the fix is a change you ship. AceMQ's senior engineers read heap dumps and class loader reports with your team, and Tomcat support runs 24/7 with response targets from 15 minutes, by plan.
Sources
- tomcat.apache.org/tomcat-10.1-doc/config/context.html, read 3 December 2026.
- tomcat.apache.org/tomcat-10.1-doc/config/listeners.html, read 3 December 2026.
- tomcat.apache.org/tomcat-10.1-doc/manager-howto.html, read 3 December 2026.
- tomcat.apache.org/tomcat-10.1-doc/class-loader-howto.html, read 3 December 2026.
- tomcat.apache.org/tomcat-10.1-doc/jndi-datasource-examples-howto.html, read 3 December 2026.
- docs.oracle.com/en/java/javase/21/docs/specs/man/jcmd.html, read 3 December 2026.
- docs.oracle.com/en/java/javase/21/docs/specs/man/java.html, read 3 December 2026.
Frequently Asked Questions
Why does Tomcat run out of Metaspace after several redeploys?
Each deployment gets its own class loader. If a thread, a ThreadLocal, a registered JDBC driver or a static reference still points at the old loader, its classes stay in Metaspace, and every redeploy adds another copy.
Does Tomcat stop Metaspace leaks on its own?
Partly. It clears ThreadLocals and RMI targets by default and ships listeners for known JRE leaks. It cannot fix a reference your application or a library creates, so a leak that survives needs a code or packaging fix.
Should I set -XX:MaxMetaspaceSize?
The JDK default is no limit. A cap turns slow growth into an early, clear OutOfMemoryError: Metaspace in the log, instead of a container kill. Size it from a measured baseline with headroom, and watch the heap and JVM memory together.
Talk to an engineer
Need a hand with Tomcat?
Senior engineers who run it in production. Get a quote or a free consultation, no sales pitch.

