Home>Article>Java> JVM Advanced Features-3. Garbage Collection Determining Object Survival Algorithm

JVM Advanced Features-3. Garbage Collection Determining Object Survival Algorithm

巴扎黑
巴扎黑 Original
2017-06-26 11:34:11 1457browse

1. Overview

In the runtime data area, the program counter, virtual machine stack, and local method stack are all created and destroyed with the thread. ’s

Therefore, their memory allocation and recycling is deterministic, and they are recycled when the method or thread ends. The Java heap and method area are uncertain. The size of the objects created during the running of the program is uncertain. The size of the required memory can only be known when the program is running.

2. "Survival Algorithm"

To determine whether an object is alive, there are two main algorithms: reference counting Method and reachability analysis algorithm

Reference counting method
  • Reference counting method is to add a reference to the object Counter, every time the object is referenced once, the counter value is incremented by 1, and the reference expiry is decremented by 1. If the counter is 0, it means it will not be used again.

reachability analysis algorithm

  • The reachability analysis algorithm is a mainstream implementation , this algorithm uses the node that becomes GC ROOT as the root node,Searches downward from the root node, if there is no reference chain connecting the object to GC ROOT (that is, it is unreachable), it means that

  • The object is not available

The objects that can be used as GC ROOT are:

  • ##Virtual machine stack The reference object in the local variable table


  • The class static property reference in the method is exclusive
    • The transparent object in the method
    • Reference object in local method

    • At present, the reachability analysis algorithm has become the mainstream algorithm. The reason is that the reference counter algorithm cannot solve the problem of

      objects referencing each other

    reachability Analysis and Judgment Process

    • After conducting the reachability analysis on the object, it is found that it does not have any reference chain connected to it, so it is marked for the first time. And perform a screening,

      The filtering condition is whether the finalize() method needs to be executed. When the object does not cover the finalize method or the finalize method has been called,
    It is considered that there is no need to execute it. If If it is judged that it is necessary to execute, it will be put into a queue called F-Queue. Later, JVM

    will automatically create a low-priority finalizer thread to execute the finalize method of these objects, and then GC will The object

    in F-QUEUE will be marked for the second time. If the object cannot escape at this time, it will be recycled.

    finalize() method

    • ## 

      The finalize method is mentioned many times above, please pay attention to it , the object's finalize method will only be automatically called once by the system.It is not recommended that you use this method. Its function can be completed with try-finally instead

    The above is the detailed content of JVM Advanced Features-3. Garbage Collection Determining Object Survival Algorithm. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn