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);
}
}