"Deep Understanding of Virtual Machine" Reading Notes-Reclaiming Memory Judgment

Reclaim memory judgment

  • Reference counting (not applicable)
    • There is a reference counter for the object. When a place refers to it, the counter is +1; if the reference is invalid, it is -1. When the counter is 0, there is no reference relationship.
    • Simple implementation and high judgment efficiency
    • There is the issue of circular references. There is a mutual reference between two objects, but it cannot be accessed externally and should be recycled, but there is a circular reference, which makes it impossible to recycle.
  • Reachability analysis algorithm (gc-roots)
    • Gc Roots
      • Objects referenced by the virtual machine stack (local variable table in the stack frame)
      • The static variable reference object in the method area
      • Constant reference object in method area
      • Local method stack JNI--native reference object
    • Through the object of "GC Roots" as the starting point, the object path is searched downward, which is called the reference chain.
    • When there is no reference chain between the object and GC Roots, the object is unreachable.
  • Reference type
    • Strong Reference
      • The reference associated with the object created by the keyword new is a strong reference.
      • When the JVM has insufficient memory space, the JVM would rather throw an OutOfMemoryError runtime error (OOM), and would not solve the problem of insufficient memory by recycling "live" objects with strong references.
    • Soft Reference
      • Only when the JVM thinks that there is insufficient memory, it will try to reclaim the object pointed to by the soft reference: that is, the JVM will ensure that the object pointed to by the soft reference is cleaned before OutOfMemoryError is thrown.
      • The soft reference can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the soft reference is collected by the garbage collector, the virtual machine will add the soft reference to the reference queue associated with it.
      • Application scenario: Soft references are usually used to implement memory-sensitive caching.
    • Weak Reference (WeakReference)
      • In the process of the garbage collector thread scanning the memory area under its jurisdiction, once an object with weak references is found, regardless of whether the current memory space is sufficient, it will reclaim its memory.
      • Weak references can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the weak reference is garbage collected, the virtual machine will add the weak reference to the reference queue associated with it.
      • Application scenario: Weak applications can also be used for memory-sensitive caches.
    • Phantom Reference (PhantomReference)
      • If the object only holds virtual references, then as with no references, any properties or functions of the object that cannot be accessed through virtual references can be collected by the garbage collector.
      • Phantom references simply provide a mechanism to ensure that after an object is finalized, it does something.
      • The virtual reference must be used in conjunction with the reference queue (ReferenceQueue). When the garbage collector prepares to reclaim an object, if it finds that it still has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory.
      • You can know whether the referenced object will be garbage collected by judging whether a virtual reference has been added to the reference queue. If you find that the virtual reference has been added to the reference queue, you can take some action before the referenced object is recycled.
    • For soft references and weak references, garbage collection may have a second confirmation problem to ensure that it has not been changed to a strong reference.
  • Judging the death of the subject
    • 1. After the accessibility analysis of the object, if there is no reference chain with GC Roots, it is marked for the first time and filtered.
    • 2. Screening condition: Is it necessary to perform finalize () on this object? When the object does not cover finalize () or has been executed, it is regarded as "no need to execute".
    • 3. If it is necessary to execute, the object is put into the F-Queue queue and executed by the low priority Finalizer thread created by the virtual machine.
      • There is no guarantee that finalize () will be executed or completed, to prevent finalize from executing slowly or in an endless loop, resulting in a crash in memory reclamation.
      • finalize () is executed at most once during the entire life cycle. If the object is recycled again after being rescued by this method, it is considered that this method has been called.
    • 4. The GC marks the objects in the F-Queue a second time on a small scale. If an object establishes a reference chain with GC Roots, it will be removed from the "coming soon" collection.
  • Method area recycling
    • Obsolete constant
      • It can be recycled without related references.
    • Useless class
      • All instances of this class have been recycled
      • The ClassLoader that loaded the class has been recycled
      • The Class object corresponding to this class is not referenced, and the methods of this class cannot be accessed through reflection.
    • Satisfying the recycling conditions of useless classes only means that they can be recycled.
Published 24 original articles · praised 0 · visits 102

Guess you like

Origin blog.csdn.net/jiangxiayouyu/article/details/105614222