Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6074509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:23:24+00:00 2026-05-23T10:23:24+00:00

I just had an interview where I was asked to create a memory leak

  • 0

I just had an interview where I was asked to create a memory leak with Java.

Needless to say, I felt pretty dumb, having no idea how to start creating one.

What would an example be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:23:25+00:00Added an answer on May 23, 2026 at 10:23 am

    Here’s a good way to create a true memory leak (objects inaccessible by running code but still stored in memory) in pure Java:

    1. The application creates a long-running thread (or use a thread pool to leak even faster).
    2. The thread loads a class via an (optionally custom) ClassLoader.
    3. The class allocates a large chunk of memory (e.g. new byte[1000000]), stores a strong reference to it in a static field, and then stores a reference to itself in a ThreadLocal. Allocating the extra memory is optional (leaking the class instance is enough), but it will make the leak work that much faster.
    4. The application clears all references to the custom class or the ClassLoader it was loaded from.
    5. Repeat.

    Due to the way ThreadLocal is implemented in Oracle’s JDK, this creates a memory leak:

    • Each Thread has a private field threadLocals, which actually stores the thread-local values.
    • Each key in this map is a weak reference to a ThreadLocal object, so after that ThreadLocal object is garbage-collected, its entry is removed from the map.
    • But each value is a strong reference, so when a value (directly or indirectly) points to the ThreadLocal object that is its key, that object will neither be garbage-collected nor removed from the map as long as the thread lives.

    In this example, the chain of strong references looks like this:

    Thread object → threadLocals map → instance of example class → example class → static ThreadLocal field → ThreadLocal object.

    (The ClassLoader doesn’t really play a role in creating the leak, it just makes the leak worse because of this additional reference chain: example class → ClassLoader → all the classes it has loaded. It was even worse in many JVM implementations, especially prior to Java 7, because classes and ClassLoaders were allocated straight into permgen and were never garbage-collected at all.)

    A variation on this pattern is why application containers (like Tomcat) can leak memory like a sieve if you frequently redeploy applications which happen to use ThreadLocals that in some way point back to themselves. This can happen for a number of subtle reasons and is often hard to debug and/or fix.

    Update: Since lots of people keep asking for it, here’s some example code that shows this behavior in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i was asked a few java interview questions which i had no idea how
I just got this question on an interview and had no idea how to
I just had a quick phone interview. The interviewer asked me a few questions
I just had an idea that I wonder whether is possible in java. Let's
I just had a test on java and we had to give the definition
I just had a look at Suns Java tutorial, and found something that totally
I just had an idea for a cool website, but it would require an
I just had an interview where one of the questions was something like Describe
I just had to re-install mysql and I am having a problem starting it
I just had a homework assignment that wanted me to add all the Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.