Garbage Collection Roots

Garbage Collection Roots — The Source of All Object Treesapp

Every object tree must have one or more root objects. As long as the application can reach those roots, the whole tree is reachable. But when are those root objects considered reachable? Special objects called garbage-collection roots (GC roots; see Figure 2.2) are always reachable and so is any object that has a garbage-collection root at its own root.ide

 

There are four kinds of GC roots in Java:spa

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

Active Java threads are always considered live objects and are therefore GC roots. This is especially important for thread local variables.orm

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

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

Figure 2.2: GC roots are objects that are themselves referenced by the JVM and thus keep every other object from being garbage-collected.rem

Therefore, a simple Java application has the following GC roots:it

  1. Local variables in the main methodio

  2. The main thread

  3. Static variables of the main class

============END============

相關文章
相關標籤/搜索