Java Virtual Machine - Garbage Collector and Memory Allocation Strategy

  • Object Death Recognition

  • reference counting algorithm
  1. Add a reference counter to the object. Whenever a place refers to it, the counter value is incremented by one; when the reference expires, the counter value is decremented by one, and the object with the counter value of 0 can no longer be used at any time. The reference counting algorithm is simple to implement and efficient, but it is difficult to solve the problem of circular references between objects.
  • Reachability Analysis Algorithms
  1. The basic idea of ​​​​this algorithm is to use a series of objects called "GC Roots" as the starting point, and start searching downward from these points. The path through which the search passes is called "reference chain". When an object does not have any reference chain to GC Roots When connected, it proves that the object is unavailable. The Java virtual machine uses this algorithm to determine whether an object is alive.
  2. The objects that can be used as GC Roots in Java objects include the following

            1. The reference object in the virtual machine stack (local variable table).

            1. The object referenced by the method area constant.

            2. The object referenced by the static property in the method area.

            3. The object referenced by the native method JNI.

  •    quote

              1. Strong Reference

                  As long as the strong reference exists, the garbage collector will never collect it (eg: Object obj = new Object())

              2. Soft Reference

                  Before the memory overflow exception occurs in the system, these objects will be listed in the recycling range for a second recycling. If the recycling is still not enough memory, an exception will be thrown (describe some useful but not necessary objects) (Expanded after JDK1.2)

              3. Weak Reference

                    Objects associated with weak references can only survive until the next garbage collection occurs. When the garbage collector works, regardless of whether the current memory is sufficient, objects only associated with weak references will be reclaimed (describe non-essential objects, JDK1.2 expanded later)

              4. Phantom Reference

                       The weakest reference relationship, whether an object has a virtual reference has no effect on its survival, and it is impossible to obtain an object instance through a virtual reference. The only purpose of setting a phantom reference association for an object is to receive a system notification when the object is reclaimed by the collector. ( Expanded after JDK1.2)

  • live or die

Even if the objects that cannot be reached by the reachability analysis algorithm are not immediately recovered, they are temporarily in the "probation" stage, and the official declaration of death needs to go through two marking processes. When the reachability analysis finds that there is no reference chain that is not connected to the GC Roots , then it will be marked and filtered for the first time (the filter condition is whether the object executes the filnalize() method, when the object does not override the filnalize() occurrence or is called by the virtual machine, it is regarded as "not necessary to execute" ), if the object judges that it is necessary to execute the filnalize() method, then the song object will be put into the FQ-queue queue, and will be executed by the low-priority flinalizer thread automatically created by the virtual machine later (and the promise will wait for it to run to the end) it, the filnalize() method is the only chance for the object to escape death, later the GC will mark the object in the D-Queue for a second small scale, if the object is to be rescued in the filnalize() method You only need to re-establish a link with any object on the reference chain.

  • Recycling method area

    1. Constants (recycling discarded constants)

              (1) To recycle an abandoned constant, you need to first make sure that the constant is not referenced elsewhere;

    2. Class (recycling useless classes)

              (1) The class does not have any instance objects in the heap.

              (2) The ClassLoader class parser of this class is recycled.

              (3) The class object of java.Lang.Class of this class is not referenced anywhere, and there is no place to access the class through reflection.

        *Note: For useless classes that meet the above three conditions, the Java virtual machine "can" recycle them, which is different from the inevitable recycle of objects. Hosp provides -Xnocalassgc parameter for control, you can also use -verbose:class and -XX:+TraceClassLoading, -XX:+TraceClassUnLoading to view the loading and unloading information of the class, of which -verbose:class and -XX:+traceClassloading can be used in the Product version For virtual machines, the -XX:+TraceClassUnLoding parameter requires the FastDebug version of the virtual machine to support. In scenarios such as a large number of proxies, dynamic reflection, ByteCode frameworks such as CGLib, dynamically generated JSPs, and frequently custom ClassLoaders such as OSGi, all have the function of class unloading to ensure that the permanent generation will not overflow.

  • Garbage Collection Algorithms

  • mark-sweep algorithm

       (1) First mark all objects that need to be recycled, and uniformly recycle the marked objects after the marking is completed.

       (2) Disadvantages: low efficiency, space problems, easy to generate a large number of discontinuous memory fragments, resulting in the need to allocate larger memory, unable to find enough continuous memory and have to trigger another garbage collection action in advance.

  • replication algorithm

        (1) Divide the memory into two pieces of memory of equal size. Only one piece of memory is used at a time. When one piece of memory is used up, the surviving objects are put into another piece of memory, and the used objects are cleared at one time.

      (2) Disadvantage: Sacrificing half of the memory

      (3) The copy collection algorithm requires more copy operations when the object survival efficiency is relatively high, and the efficiency is low

  • mark-collate algorithm
       (1) Mark the object, then move the surviving object to one end, and then clear the memory outside the boundary
  • Generational Collection Algorithm

       (1) Divided into new generation and old generation

       (2) The new generation adopts the replication algorithm, and the old generation adopts the clear marking or marking sorting algorithm.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625581&siteId=291194637