Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

After about 30 redeploys of a WAR in a long-running Tomcat without a restart, the JVM fails with OutOfMemoryError: Metaspace. Describe how you find what is holding the old web application class loaders.

This is a class loader leak: every redeploy creates a new web application class loader, and something outside the application still references the old one, so none of its classes can be unloaded and metaspace fills after about 30 copies. We take a heap dump, find the web application loader instances that belong to stopped applications, and follow the shortest path to GC roots, excluding weak and soft references, to see the holder. Typical holders are ThreadLocal values on container threads, threads the application never stopped, JDBC drivers left in DriverManager, shutdown hooks, MBeans and static caches in libraries loaded by the container.

Why this question is asked

Redeploying a WAR without restarting Tomcat is convenient, and for years it was how many teams released. The cost shows up later as an OutOfMemoryError: Metaspace that seems to have nothing to do with the code that was just deployed. Interviewers ask this debugging question because it tests reachability reasoning, heap dump skills, and knowledge of the usual culprits. In this answer, we will recap why redeploys can leak, confirm the leak with jcmd, find the holder in a heap dump, go through the common holders and their fixes, and see what Tomcat itself reports.

1A Quick Recap: Why a Redeploy Can Leak

Tomcat gives each deployed web application its own class loader, which defines all the classes in WEB-INF/classes and WEB-INF/lib, often 10,000 or more classes and 50 to 150 MB of metadata in metaspace for a Spring application. On redeploy, Tomcat stops the application, discards its loader, and creates a new loader for the new version.

The old loader and all its classes can be freed only when nothing reachable refers to the loader, any of its classes, or any object of those classes. Tomcat itself lives on, with its own threads and its own classes in parent loaders. If any of those still holds one object from the old application, the whole old loader stays. After 30 redeploys, 30 copies of the application's classes sit in metaspace, and the limit is reached.

Step throughHow one reference pins a whole application
  1. Tomcat worker threadlives across redeploys: a GC root
  2. Thread's ThreadLocal mapvalue left by the old application
  3. Value objectinstance of com.acme.RequestContext
  4. Its Classcom.acme.RequestContext of the old version
  5. Old ParallelWebappClassLoaderstopped, but reachable
  6. All 12,000 classes of the old versionabout 90 MB of metaspace

6 layers, from the top down. Step through them one at a time.

2Confirming the Leak

First we confirm that old loaders are piling up. jcmd lists class loaders with their class counts and metaspace use.

Shell
$ jcmd 4242 VM.classloader_stats | grep WebappClassLoader
0x0000000800f3a1c0 0x000000080001e2b8 ... 12043 94371840 ... org.apache.catalina.loader.ParallelWebappClassLoader
0x0000000800e1c5a8 0x000000080001e2b8 ... 12043 94371840 ... org.apache.catalina.loader.ParallelWebappClassLoader
0x0000000800d22f70 0x000000080001e2b8 ... 12043 94371840 ... org.apache.catalina.loader.ParallelWebappClassLoader
// ... 30 lines for one deployed application: 29 of them should be gone
$ jcmd 4242 VM.metaspace // total metaspace and how much belongs to each loader type

Thirty web application loaders with identical class counts for one deployed application is the proof. If we force a full GC with jcmd 4242 GC.run and the count stays the same, the old loaders are not just waiting for a collection; they are reachable.

3Finding the Holder in a Heap Dump

Next we take a heap dump with jcmd 4242 GC.heap_dump /tmp/tomcat.hprof and open it in a heap analyser such as Eclipse Memory Analyzer. We list all instances of ParallelWebappClassLoader (or WebappClassLoader in older Tomcat versions). Each has a state field: the live one is STARTED, and the leaked ones are STOPPED or DESTROYED, which tells us which instances to examine.

For one stopped loader, we ask the tool for the path to GC roots, excluding weak and soft references. Excluding them matters because weak and soft references do not prevent collection, and including them buries the real path under noise. The tool then shows a chain such as the one below.

Java
Path to GC roots (excluding weak/soft references) for ParallelWebappClassLoader @ 0x8c4e1a20 (state STOPPED)
Thread "http-nio-8080-exec-7" <- GC root, a Tomcat worker thread
'- threadLocals : ThreadLocal$ThreadLocalMap
'- table[11] : ThreadLocalMap$Entry
'- value : com.acme.web.RequestContext <- object of the old application's class
'- <class> com.acme.web.RequestContext
'- <classloader> ParallelWebappClassLoader @ 0x8c4e1a20

This one is a ThreadLocal set by the application's filter and never removed. The worker thread belongs to Tomcat and survives the redeploy, so its ThreadLocal map still contains the old RequestContext. The fix is to call remove() in a finally block in the same filter that called set().

4The Usual Holders and Their Fixes

After the first fix, there is often a second holder, so we repeat the path to GC roots until the old loader is collected. The common holders are few, and each has a standard fix.

Common holders of an old web application loader
HolderHow it holds the loaderFix
ThreadLocal on worker threadsvalue is an app objectremove() in finally
Thread started by the apprunning thread, its context loaderstop executors and timers on shutdown
JDBC driver in WEB-INF/libDriverManager lists the driverderegister it, or move driver to Tomcat lib
Shutdown hookRuntime holds the hook threadremove the hook on stop
MBeansplatform MBean server holds themunregister on stop
Static cache in a parent-loaded libstatic map holds app objects or classesclear it, or bundle the lib in the WAR

Notice the pattern behind all of them: something with a longer life than the application, a container thread, a JDK-wide registry such as DriverManager or the platform MBean server, the JVM's shutdown hook list, or a class loaded by a parent loader, keeps a reference down into the application. The rule for application code is that whatever it registers outside itself, it unregisters when it stops, typically in a ServletContextListener's contextDestroyed or a Spring bean's destroy method.

5What Tomcat Reports and Why a Bigger Limit Is Not a Fix

Tomcat helps in two ways. When an application stops, it checks for many of these holders and logs warnings, for example that the web application appears to have started a thread it has failed to stop, or registered a JDBC driver and failed to unregister it, and that this is very likely to create a memory leak. It also clears some references itself, such as renewing its worker threads. And the Manager application has a Find leaks feature, which forces a GC and lists applications that were stopped or undeployed but whose loaders are still in memory. Reading the warnings at undeploy time often names the culprit before we even take a heap dump.

Raising -XX:MaxMetaspaceSize only moves the failure from the 30th redeploy to the 60th. Until the holder is fixed, the safe operational practice is to restart Tomcat on deploy, which is also what most teams do today.

6Follow-up questions

How do you prevent this class of issue in a Spring Boot executable jar deployment?

By not redeploying inside a running JVM at all. A Spring Boot executable jar runs one application per process, and a new version is deployed by starting a new process and stopping the old one, usually as a rolling update of containers. The old process exits, so every loader, thread and registry entry disappears with it and nothing can leak across versions. Run-time class generation inside one process can still grow metaspace, so we still watch metaspace and the loaded-class count, but redeploy leaks are gone by design.

Why does a JDBC driver in WEB-INF/lib cause a leak?

When the driver class is loaded, it registers itself with java.sql.DriverManager, which is a JDK class with a static list of drivers. That list now holds an object of a class defined by the web application loader. Unless the application deregisters the driver when it stops, the JDK keeps the loader alive. Putting the driver in Tomcat's lib directory, so it is loaded by the common loader, avoids the issue.

7Conclusion

In this answer, we saw that OutOfMemoryError: Metaspace after repeated redeploys is a class loader leak: each redeploy creates a new loader, and something longer-lived keeps the old one reachable. We confirmed it with jcmd VM.classloader_stats, took a heap dump, picked a stopped web application loader and followed its path to GC roots without weak and soft references. The usual holders are ThreadLocal values on container threads, unstopped threads, JDBC drivers, shutdown hooks, MBeans and static caches in parent-loaded libraries, Tomcat's undeploy warnings and Find leaks feature help find them, and raising the metaspace limit only delays the failure.

What to say in the interviewAbout 90 seconds aloud

Open with

This is a class loader leak. Each redeploy creates a new web application class loader with all the application's classes, and the old loader can only be unloaded if nothing reachable refers to it, its classes, or any object of its classes.

Then, if they want more

Something outside the application still holds one, so after about 30 redeploys there are 30 copies of the classes in metaspace. First I confirm it with jcmd VM.classloader_stats: if I see many ParallelWebappClassLoader instances with the same class count for one application, even after a forced GC, they are leaked.

Then I take a heap dump with jcmd GC.heap_dump, open it in Eclipse MAT, find the loader instances whose state is stopped, and ask for the shortest path to GC roots excluding weak and soft references. That path names the holder.

The usual ones are a ThreadLocal value left on a Tomcat worker thread, a thread or executor the application started and never shut down, a JDBC driver in WEB-INF/lib registered in DriverManager, a shutdown hook, MBeans, or a static cache in a library loaded by Tomcat's common loader that holds application objects.

I also read Tomcat's warnings at undeploy, which often name the leaking thread or driver, and the Manager's Find leaks feature lists applications whose loaders are still in memory. Raising MaxMetaspaceSize only delays the failure; the real fix is to remove the reference, and in the meantime restart Tomcat on each deploy.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "After about 30 redeploys of a WAR in a long-running Tomcat without a restart, the JVM...": https://scrivio-interview-library.netlify.app/java/class-loading/f58c3099a0/