Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread as a GC root

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?

like image 442
alobodzk Avatar asked Oct 07 '14 09:10

alobodzk


1 Answers

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.

like image 181
Stephen C Avatar answered Sep 19 '22 04:09

Stephen C