Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Thread Objects are never Garbage Collected

843798Dec 19 2006 — edited Apr 16 2007
I found something interesting the other day. If you run the following program under NetBeans 5.5 using the memory profiler you can see that Thread objects (and their subclasses) never get garbage collected. You have to wait for some of the threads to stop and manually invoke the garbage collector.

I created a bug-report on this since I think it's pretty basic that the runtime should garbage collect Thread objects after the run() method has returned.

I ran into this why trying to track down an elusive memory leak in our software.

Am I missing something? Why has this not been reported before?
public class Threads extends Thread
{
    public Threads()
    {
    }

    public void run()
    {
        System.out.println(getName() + " - starting");
        try
        {
            sleep((int) (Math.random() * 100000));
        }
        catch (InterruptedException e)
        {
                
        }
        System.out.println(getName() + " -         stopping");
    }

    public static void main(String [] arguments)
    {
        for (int i = 0; i < 100; i++)
        {
            Threads threads = new Threads();
            threads.start();
            try
            {
                Thread.sleep(2000);
            }
            catch (InterruptedException e)
            {
                
            }
        }

        System.exit(0);
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 14 2007
Added on Dec 19 2006
14 comments
433 views