003-JVM – Marking Phase Algorithms: How to determine garbage juejin.cn/post/690816…
What are gc Roots? This is the interview question, also want to recite down, if you don’t want to enter the big factory, you can not recite.
- Conventional gc roots
1, the JVM stack
Object referenced in the virtual machine stack
For example: parameters, local variables, etc. used in methods called by each thread
2. Nativ method stack
Objects referenced by JNI (commonly referred to as local methods)
3. Runtime constant Pool
Run-time constant pool
4, Static References in method area
The object referenced by the class static property in the heap JDK8
Example: Java class reference type static variable
Object referenced by a constant in (heap JDk8)
For example, references in the String constant pool
5, Clazz
Internal references of the Java VIRTUAL machine.
Class objects corresponding to basic data types, some resident exception objects (NullPointerException, OutOfMemoryError), system Class loaders
6. All objects held by synchronized
In addition to these fixed COLLECTION of GC Roots, other objects can be added “temporarily” to form a complete collection of GC Roots, depending on the garbage collector selected by the user and the memory region currently reclaimed. For example, generational collection and Partial collection (Partial GC) if garbage collection is only for a certain area of the Java heap (for example: Typical only for the new generation), it must be considered that the memory region is the implementation details of the VIRTUAL machine itself, not to mention isolated and closed. Objects in this region may be referenced by objects in other regions. At this time, it is necessary to add the associated region objects into GC Roots collection. In order to ensure the accuracy of the accessibility analysis. Local collection: means that if the collection is generational, such as for the young generation, then the variable in the old generation (which refers to the young generation). , also need to put in Roots Set. Otherwise there will be friendly fire! Tip: Since Root stores variables and Pointers on a stack, a pointer that holds objects in the heap but is not stored in the heap itself is Root.
- Temporary gc roots
In addition to these fixed COLLECTION of GC Roots, other objects can be added “temporarily” to form a complete collection of GC Roots, depending on the garbage collector selected by the user and the memory region currently reclaimed. Examples are: Generational collection and Partial GC
If you do garbage collection for only one area of the Java heap (for example: Typical only for new generation), must consider the memory region is a virtual machine implementation details, more is not isolated, sealed the area of the object can be referenced in other parts of the object, then you need will be along with all the associated region object also add to the collection of GC Roots to consider, to ensure the accuracy of the accessibility analysis. Local collection: means that if the collection is generational, such as for the young generation, then the variable in the old generation (which refers to the young generation). , also need to put in Roots Set. Otherwise there will be friendly fire! Tip: Since Root stores variables and Pointers on a stack, a pointer that holds objects in the heap but is not stored in the heap itself is Root. (This is a loose term: “temporarily” added, as mentioned above; In jdk8, StringTable, Static variables, etc., are stored in the heap. But can be used as a general criterion)
- conclusion
Regular 6 require special two in the roots set.
This article is optimized by Jianyue SimpRead to improve the reading experience using the new Jianyue lexical analysis engine Beta. Click here for more details
- Conventional gc roots
- Temporary gc roots
- conclusion