HardExperience level: 8-15 years1 min read

What causes a ClassLoader leak on application redeploy, and how do you fix it?

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftDeloitte
#classloader#memory leak#redeploy#tomcat#metaspace#production
Report issue

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.

On this page

Concept Made Simple

Coffee Chat Question

“What causes a ClassLoader leak on application redeploy, and how do you fix it?”

Remember It Faster

Mind Map Answer

Pin sources
ThreadLocals, JVM threads, JDBC drivers
Also
static caches, shutdown hooks, MBeans
Symptom
Metaspace grows per redeploy → OOM

Think Beyond the Expected

What If?

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:

  • per-webapp classloader
  • external strong ref pins it
  • ThreadLocal/driver/thread/static causes
  • Metaspace growth
  • MAT path-to-GC-roots

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

  1. Why does deregistering the JDBC driver on shutdown matter?
  2. How is this different from a normal heap leak?
  3. How does Metaspace differ from the old PermGen?

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.