What causes a ClassLoader leak on application redeploy, and how do you fix it?
Reviewed by Gurusankar M.
β‘ Short Answer
On undeploy the old web-app ClassLoader should be GC'd. If anything outside the app (a JVM-wide thread, ThreadLocal, JDBC driver, or static cache) still references a class loaded by it, the whole ClassLoader (and all its classes) is pinned β leaking Metaspace each redeploy until OOM.
βCoffee Chat Question
Concept Made Simple
βWhat causes a ClassLoader leak on application redeploy, and how do you fix it?β
π§ Mind Map Answer
Remember It Faster
π₯What If?
Think Beyond the Expected
How do you actually find what's pinning the old ClassLoader?
Heap-dump after a couple of redeploys and use MAT's 'Path to GC Roots' on the leaked WebappClassLoader instances β it shows the exact reference chain (e.g. a Thread's ThreadLocal or a static driver list) holding it.
πReal World
The textbook case: an app registers a JDBC driver but never deregisters it, so the JVM's DriverManager (a system class) keeps a strong reference to the app's driver class β pinning the whole app ClassLoader on every redeploy.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βNot deregistering JDBC drivers / stopping threads on shutdown
- βLibrary ThreadLocals left set on container threads
- βStatic caches holding app-loaded classes
β Best Practices
- βClean up in ServletContextListener.contextDestroyed (threads, drivers, ThreadLocals)
- βMonitor Metaspace; alert on per-redeploy growth
- βPrefer full restarts over hot redeploys in production
πFollow-up Questions
- 1Why does deregistering the JDBC driver on shutdown matter?
- 2How is this different from a normal heap leak?
- 3How does Metaspace differ from the old PermGen?
π§©Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: JVM/Class loading (Core Java) Interview question: "What causes a ClassLoader leak on application redeploy, and how do you fix it?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.