Reputation: 1334
I have a question about GC roots. I read that one of the GC roots is "Live thread". What does that mean?
I always had the impression that each thread has its own stack and local variables of a stack are GC roots for thread, and now I'm confused. What other types of object references, which are not on frame stack or native stack does thread representation have?
Another question is does young generation collection use GC roots, or it's only for major algorithms?
Thanks
Update: Ok sorry, so to keep it simple: I've read this short article: yourkit.com/docs/java/help/gc_roots.jsp and there's a "Thread" option as GC root, what exactly does that mean that thread is a GC root? What kind of objects are referenced by Thread GC root, that are not referenced by its stack? Why are those two categories different?
Upvotes: 10
Views: 2868
Reputation: 719739
I read that one of GC roots is "Live thread". What does that mean?
A live thread is a thread that has been started, and has not yet terminated.
What other types of object references, which are not on frame stack or native stack does thread representation have?
None.
When they say that a (live) thread is a GC root, they mean (in effect) all of the values in the thread's stack frames.
(The "frame stack" and the "native stack" hold essentially the same information in different modes of execution; i.e. interpreted versus compiled. There is no need to make a distinction between them when understanding GC roots.)
... what exactly does that mean that thread is a GC root?
It means that thread's stack is a GC root, and the contents of all live variables in all of the thread's stack frames are reachable.
These things are all saying effectively the same thing.
Another question is does young generation collection use GC roots, or it's only for major algorithms?
(First it should be noted that not all Java GCs are generational, and that any generalizations we make could be rendered incorrect by new GC technology.)
A young generation collection certainly does. It needs to know what is in all of the roots in order to avoid deleting objects referred to by those roots. Since the GC roots can refer to objects in the young generation, the young gen collector needs to use them.
In one sense all collectors use them. But in another sense, the GC roots are only directly used during some of the "stop the world" collection phases. For those collectors / phases that run while normal threads are running, the user threads may modify the GC roots. The GC infrastructure uses things like write barriers to capture any changes that may affect reachability ... in various ways.
Upvotes: 6
Reputation: 70999
The JVM partitions its threads, some are exclusively used for Garbage Collection, some are for other internal JVM tasks, and some execute the user supplied portion of the executable.
In this context, reachable means reachable by the user execution threads. This includes the first thread which is bound to run from public static void main(String[] args)
and all the threads launched from that thread, minus the ones that become unreachable or complete.
Upvotes: 0
Reputation: 47
Imagine a method with a java thread object local new'ed , when the method exits the object is gone (the reference goes out of scope and any heap allocated memory eligible for GC). If in the same method you start the thread, now the live time of that thread object and anything it references is also tied to the life time of the live \ running thread. Until the thread exits any memory still referenced from the running thread is ineligible for GC and the thread said to be a GC root.
Threads can allocate memory in two different ways via the stack or the heap. Stack storage is not GC'ed but reclaimed when the current stack frame is unwound. Heap storage is typically allocated when you use 'new' in your code (note new does not always mean heap storage see Escape Analysis). The heap is GC'ed.
A good way to learn more about GC roots is to take a heap dump of your running java application and load it into Visual VM or Eclipse MAT, from there you should be able to examine the GC roots.
Young generation collection would use GC roots in that objects with GC roots are not eligible for GC but it would be better to talk in terms of a given algorithmn.
Upvotes: 0