According to this article How Garbage Collection Works there are four kinds of Gc roots:
- Local variables are kept alive by the stack of a thread. This is not a real object virtual reference and thus is not visible. For all intents and purposes, local variables are GC roots.
- Active Java threads are always considered live objects and are therefore GC roots. This is especially important for thread local
variables.- Static variables are referenced by their classes. This fact makes them de facto GC roots. Classes themselves can be garbage-collected, which would remove all referenced static variables. This is of special importance when we use application servers, OSGi containers or class loaders in general. We will discuss the related problems in the Problem Patterns section.
- JNI References are Java objects that the native code has created as part of a JNI call. Objects thus created are treated specially because the JVM does not know if it is being referenced by the native code or not. Such objects represent a very special form of GC root, which we will examine in more detail in the Problem Patterns section below.
In JVM specification local variables in the frames of stack have no type and it's just somehow an array of bytes and it's the responsibility of compiler to generate type specific instruction for those local variables for instance iload, fload, aload, etc. So clearly GC can not find references to object by only looking at local variable section of the stack frames.
My questions are :
How GC finds those roots at all?
How GC can find local variables in the stack that are references to object and are not other type of variables (for instance variables that have been stored by iconst)?
Then How Gc finds fields of those objects to create an accessible tree?
Does it use instruction that are defined by JVM itself to find those objects?
And lastly what is the meaning of this sentence in the article?
This is not a real object virtual reference and thus is not visible