A: 003 – the JVM – mark phase algorithm: how to determine the garbage https://blog.csdn.net/fsjwin/article/details/111322134

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.

1. Conventional GC roots

    1. JVM Stack Refers to objects in the virtual machine stack, such as parameters and local variables used in methods called by individual threads
    1. Nativ Method Stack Objects referenced by JNI
    1. Runtime Constant Pool Runtime constant pool
    1. Static references in method area (heap JDk8) static references in method area (heap JDk8) static references in method area (heap JDk8) static references in method area
    1. Clazz Internal reference of the Java VIRTUAL machine. Class objects corresponding to basic data types, some resident exception objects (NullPointerException, OutOfMemoryError), system Class loaders
    1. 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 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.

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

  1. If you do garbage collection for only one area of the Java heap (for example, typically only for the new generation), you must consider that the memory area is an implementation detail of the virtual machine itself, and is not isolated. Objects in this area may well be referenced by objects in other areas, so you need to do this altogetherThe associated region objects are also added to the GC Roots collectionIn order to ensure the accuracy of the accessibility analysis.
  2. Partial recycling: meaning that if you recycle by generation, for example from the younger generation,So 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)

3. Summary

  1. Regular 6 medium is required in roots set
  2. Special two in the roots set.

Next article: 005 – the JVM – clear stages: pure discussion of garbage collection algorithm for https://blog.csdn.net/fsjwin/article/details/111326292