Thread as a GC root

I have a question about GC roots. I read that on of GC roots is "Live thread". What does that mean? I always had impression that each thread has it's 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?

Other question is does young generation collection uses 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 it's stack? Why are those two categories different?


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" are the same thing.)

... 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.


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.


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.

链接地址: http://www.djcxy.com/p/19888.html

上一篇: GC的根源是什么?

下一篇: 线程作为GC根