Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is a dead thread a threat to memory leak?

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting the thread object to null should guarantee GC on the dead thread object.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic