[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Memory leak in java.lang.Thread ?
From: |
Jeroen Frijters |
Subject: |
RE: Memory leak in java.lang.Thread ? |
Date: |
Wed, 8 Dec 2004 10:01:00 +0100 |
Archie Cobbs wrote:
> Archie Cobbs wrote:
> > What happens when you run this program:
> >
> > public class ThreadLeak {
> > public static void main(String[] args) {
> > while (true)
> > new Thread();
> > }
> > }
> >
> > I get an OutOfMemoryError.
>
> Now I realize that Sun's JDK has the same bug:
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4410846
>
> Can we fix this simply by having the references from the
> ThreadGroup -> Thread be via weak references?
>
> I'm not sure why Sun hasn't done that already.
Because it would mean that the semantics change. There could be code
that does:
new Thread("MyThread");
...
...
ThreadGroup g = Thread.currentThread().getThreadGroup();
Thread[] threads = new Thread[g.activeCount()];
g.enumerate(threads);
for(int i = 0; i < threads.length; i++)
{
if(threads[i].getName("MyThread"))
threads[i].start();
}
Now I don't know why anyone would want to do this, but I don't see why
you'd do while(true) new Thread() either ;-)
Regards,
Jeroen