aspose file tools*
The moose likes Threads and Synchronization and the fly likes Live thread and GC root set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Live thread and GC root set" Watch "Live thread and GC root set" New topic
Author

Live thread and GC root set

chatchai chailuecha
Greenhorn

Joined: Dec 14, 2006
Posts: 18
I came across a
thread
asking:

Thread t = new Thread(); t.start(); t = null; now what will happen to the created thread?


One of the replies was that:
As long as it's not dead, it belongs to a ThreadGroup, which keeps it from being collected.


Is it true that a live thread doesn't get collected by GC just because
there is a reference from ThreadGroup to it?

I tried to prove it by using this program



Here was the result
java -Xmx100m -Xms80m -verbose:gc -cp classes Test
Thread started
Active thread count2
Active thread count2
Active thread count2
Remove thread from thread group
Done
[GC 307K->107K(81344K), 0.0015450 secs]
[Full GC 107K->107K(81344K), 0.0093870 secs]
Active thread count1
Active thread count1
Active thread count1

GC seemed to ignore the live thread even if there was no reference
from the thread group to it.

Will it make more sense if the real reason is something like all
live threads will be automatically added to GC root set and therefore
keep them from being collected even if there was no reachable reference
to it?

Thanks very much.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19062
    
  40

GC seemed to ignore the live thread even if there was no reference
from the thread group to it.


A live thread is more than just a thread object which is stored in some class. There is stack memory allocated to it too. Not to mentioned thread stuff at the OS layer.

You may be able to force the thread object to be dereferenced from the thread group, but you didn't delete the root. A "root" is defined as a starting point to check for reachability. There are many types for roots, and the thread's stack is one of them.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
chatchai chailuecha
Greenhorn

Joined: Dec 14, 2006
Posts: 18
Henry, Thanks very much for the clarification
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Live thread and GC root set