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 1004999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:13:18+00:00 2026-05-16T08:13:18+00:00

I am referring to this code example, which is being reported in https://bugs.java.com/bugdatabase/view_bug?bug_id=6254531 import

  • 0

I am referring to this code example, which is being reported in https://bugs.java.com/bugdatabase/view_bug?bug_id=6254531

import java.net.URL;
 
class Loader {
    public static void main(String[] args) throws Exception {
        for (;;) {
            System.gc();
            System.out.print(".");
            System.out.flush();
            new java.net.URLClassLoader(
                new URL[] { new java.io.File(".").toURL() },
                ClassLoader.getSystemClassLoader().getParent()
            ).loadClass("Weakling").newInstance();
        }
    }
}
public class Weakling {
    private static ThreadLocal<Object> local;
    private static Weakling staticRef;
    private Object var = new byte[1000*1000];
    public Weakling() {
        local = new ThreadLocal<Object>();
        local.set(this);
        staticRef = this;
    }
     
    @Override
    protected void finalize() {
        System.out.print("F");
        System.out.flush();
    }
}

The finalize will never be called. However, if I change the

            new java.net.URLClassLoader(
                new URL[] { new java.io.File(".").toURL() },
                ClassLoader.getSystemClassLoader().getParent()
            ).loadClass("Weakling").newInstance();

to

new Weakling();

It works very well and no leaking detected.

Can anyone explain why the object created by ClassLoader do not have chance to garbage collect itself?

  • 1 1 Answer
  • 2 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-16T08:13:19+00:00Added an answer on May 16, 2026 at 8:13 am

    The ThreadLocal mechanism effectively stores on the current thread a WeakHashMap of ThreadLocal instances to values. Consequently, if the ThreadLocal instance never becomes weakly referenceable, then the entry is effectively leaked.

    There are two cases to consider. For the simplicity of discussion, let’s assume that ThreadLocal actually stores a WeakHashMap on Thread.currentThread(); in reality, it uses a more sophisticated mechanism that has an equivalent effect.

    First consider the “new Weakling” scenario:

    • On the first iteration of the loop:
      1. the Weakling class is loaded from the system class loader
      2. the Weakling constructor is called
      3. the Weakling.local static variable is set from null to a new ThreadLocal instance #1
      4. the ThreadLocal WeakHashMap is updated to store the new Weakling instance #1
    • On all subsequent iterations of the loop:
      1. the Weakling class is already loaded from the system class loader
      2. the Weakling constructor is called
      3. the Weakling.local static variable is set from the old ThreadLocal instance #1 to a new ThreadLocal instance #2. The old ThreadLocal instance #1 is now only (weakly) referenced by the WeakHashMap.
      4. the ThreadLocal WeakHashMap is updated to store the new Weakling instance. During this operation, the WeakHashMap notices that the old ThreadLocal instance #1 is only weakly referenceable, so it removes the [ThreadLocal instance #1, Weakling #1] entry from the Map before it adds the [ThreadLocal instance #2, Weakling #2] entry.

    Second consider the “new URLClassLoader(…).loadClass(…).newInstance()” scenario:

    • On the first iteration of the loop:
      1. the Weakling class #1 is loaded from URLClassLoader #1
      2. the Weakling constructor is called
      3. the Weakling.local #1 static variable is set from null to a new ThreadLocal instance #1
      4. the ThreadLocal WeakHashMap is updated to store the new Weakling instance #1
    • On all subsequent iterations of the loop
      1. the Weakling class #n is loaded from URLClassLoader #n
      2. the Weakling constructor is called
      3. the Weakling.local #n static variable is set from null to a new ThreadLocal instance #n
      4. the ThreadLocal WeakHashMap is updated to store the new Weakling instance.

    Note that during this final step, ThreadLocal instance #1 is not weakly referenceable. This is because of the following reference chain:

    • WeakHashMap value strongly references Weakling instance #1
    • Weakling instance #1 strongly references Weakling class #1 via Object.getClass()
    • Weakling class #1 strongly references ThreadLocal instance #1 via the static class variable

    As long as the loop continues to run, more entries are added to the ThreadLocal WeakHashMap, and the strong reference chain from value-to-key (Weakling instance to ThreadLocal) in the WeakHashMap prevents garbage collection of otherwise stale entries.

    I’ve modified the Loader program to iterate 3 times and then wait for user input. Then, I generated a heap dump using java -Xrunhprof:heap=dump and ctrl-pause/break. The following is my analysis of the final heap dump:

    First, there are three Weakling objects:

    OBJ 500002a1 (sz=16, trace=300345, class=Weakling@50000296)
    OBJ 500003a4 (sz=16, trace=300348, class=Weakling@5000039d)
    OBJ 500003e0 (sz=16, trace=300342, class=Weakling@500003d9)
    

    Note that all three Weakling instances (500002a1, 500003a4, and 500003e0) are created from three distinct class instances (50000296, 5000039d, and 500003d9, respectively). Looking at the first object, we can see that it is held as a value in an entry object in the threadLocal map:

    OBJ 500002a5 (sz=32, trace=300012, class=java.lang.ThreadLocal$ThreadLocalMap$Entry@5000014b)
            referent        500002a4
            queue           500009f6
            value           500002a1
    

    The referent here is value being held weakly:

    OBJ 500002a4 (sz=16, trace=300347, class=java.lang.ThreadLocal@50000125)
    

    Searching, we can see that this object is held as a value in the static
    variable “local” of the aforementioned Weakling class:

    CLS 50000296 (name=Weakling, trace=300280)
            super           50000099
            loader          5000017e
            domain          50000289
            static local    500002a4
            static staticRef        500002a1
    

    In conclusion, we have the following strong reference chain loop for this Weakling instance, which prevents it from being garbage collected.

    • WeakHashMap value (500002a5) strongly references Weakling instance (500002a1)
    • Weakling instance (500002a1) strongly references Weakling class (50000296) via Object.getClass()
    • Weakling class (50000296) strongly references ThreadLocal instance (500002a4) via the static class variable

    A similar analysis on the other Weakling objects would show a similar result. Allowing the program to run for additional iterations shows that the objects continue to accumulate in this manner.

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

Sidebar

Related Questions

Given this JavaScript code (which is just a comment referring to a url): //
I am referring to this project by Jimmy Bogard: http://www.codeplex.com/AutoMapper The code repository site
Referring to this question: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me class Form { protected $inputs = array(); public function
i am referring to an Fusion Tables API Example „Update Query https://developers.google.com/fusiontables/docs/samples/change_query It's is
I get this problem when referring to OpenFEC (openfec.org)-functions in my code: anders@ubuntu:~/workspace/fectest$ gcc
Referring to this web site http://www.cplusplus.com/reference/std/utility/make_pair/ The std::make_pair has this signature (and possible implementation):
This is what I'm referring to: http://twitter.github.com/bootstrap/components.html#badges Is badge the commonly agreed upon term?
i'm referring this address for function olLiTree PHP function that creates a nested ul
Referring to this question , how can i get the current page size in
referring to this question , I've decided to duplicate the tables every year, creating

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.