What causes a ClassLoader leak on application redeploy, and how do you fix it?
⚡ 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.