aspose file tools*
The moose likes Threads and Synchronization and the fly likes Is a dead thread a threat to memory leak? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is a dead thread a threat to memory leak?" Watch "Is a dead thread a threat to memory leak?" New topic
Author

Is a dead thread a threat to memory leak?

Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Hi guys,

I am new to threads. I read it from my SCJP book says that once a thread finish the run() method, it's consider dead. However, I still see the thread in the debug perspective in Eclipse IDE.

my question is, is a dead thread a threat to memeory usuage? will it eventually get GC?

Thanks
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

Do you mean the thread as in OS thread or as in Java object used to represent it, obviously if you code still has a reference the thread object is still there, you can't restart it but its still a usable java object (sorry if that was too obvious, best to eliminate that first ;-) )

Which view in eclipse ?


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

Setting the thread object to null should guarantee GC on the dead thread object.


S.D. MADHAN
Not many get the right opportunity !
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

If its the only reference it'll make it available for GC. The thread will also have been loitering off a thread group, ... again should all have cleaned up nicely though.
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
It's the debug view.

our code is



this code is placed in a parent object's constructor. the parent object is a extension of JDialog.
when the parent object is closed, we call dispose() to it. But in the Eclipse debug perspective or view I still see the thread there (identified by some number generated by eclipse). Every time we open the parent object and a new thread is generated. but it just doesn't seem to go away when parent object is closed. As you can see, the thread reference is local, maybe my question should be, should we set this.someRunnable to null when the parent is closed.

Thanks
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

I'm assuming the real thread has completed should be easy to check.

Obviously that runnable reference as long as it exists and isn't null prevents GC , thread has a reference to it also and that will be kept un GC'd by a thread group till it completes ??. Can you post a simple example that exhibits the problem and I'll (or some one will) fire up Eclipse and tell you exactly what's going on ;-)
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Hi Chris,

thanks for your reply, I thought the code snippet I included at the last post was enough for running a simple example in eclipse.
what else would I need to post? or do I need to describe what the runnable is doing? I would like to be careful not to expose too much of my company's code. hope you understand.

Thanks again.
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
I got the issue resolved. what we did was we set a flag in runnable when the window is closed to make the flag to true which complete the run() method in runnable. once the run() method completes, the thread is GC'd.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is a dead thread a threat to memory leak?
 
Similar Threads
Restarting Thread
Thread
Y do u do this 2 ur posts
beautiful moments of life...
eForm