Talk about the JVM garbage collection

Outline

Java Runtime area, the program counter, the virtual machine stack, native method stacks three regions with the thread was born, and died with threads, memory allocation and recovery of these areas are equipped with certainty, does not require much thought recycling issues. The Java heap and method area is not the same, more of a class that implements the interface of memory needed is not the same, a method of multiple branches may not be a need for memory, we can only run in order to know the object will be created, memory allocation and recovery of this part of the garbage collector is concerned. The garbage collector needs to complete three issues: the need to reclaim that memory; when and how to recycle recycling.

Those who refuse to be recovered

The basic idea of ​​garbage collection is to examine an object up, that is starting from the root that you can access to this object, if you can, then the object is being used, on the contrary can not be accessed from the root to the object, the object has been explained no longer in use, in general, the object is to be recovered. This algorithm is the root search algorithm.

Reachability analysis

In practice, however, a possible target unreachable "Resurrection" under certain conditions themselves, so its recovery is unreasonable. To this end we give the definition of an object reachability states, and the provisions under which the state can be safely recycled objects. Accessibility object contains the following three states.

  • Reachable: starting from the root, according to the reference node, you can search for this object
  • Be resurrected: All references to objects have been released, but the object may finalize()revive their own methods.
  • Unreachable: the object finalize()method is called, and has not been raised, then enter unreachable. Unreachable object can not be "resurrected" because the finalize()method can only be called once.
/**
 * 
 * <p>Description: 1.对象被GC时,可以通过finalize拯救 2.finalize只被调用一次 </p>  
 * @date 2019年8月25日  
 * @version 1.0
 */
public class FinalizeTest {

    private static FinalizeTest currentObj;

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize invoke");
        //重新引用
        currentObj = this;
    }

    public void alive() {
        System.out.println("live");
    }

    public static void main(String[] args) throws InterruptedException {
        currentObj = new FinalizeTest();

        currentObj = null;
        System.gc();
        //finalize优先级地,先等待
        Thread.sleep(500);
        if(currentObj == null) {
            System.out.println("dead");
        }else {
            currentObj.alive();
        }

        currentObj = null;
        System.gc();
        //finalize优先级地,先等待
        Thread.sleep(500);
        if(currentObj == null) {
            System.out.println("dead");
        }else {
            currentObj.alive();
        }
    }
}

The above code is broken as a code fragment, but the result was not the same, the first object "rescue resurrection" success, another failure, it can normally be recycled.

As GC Roots of the following categories:

  • Virtual Machine stack (Local Table scale stack frame) referenced objects
  • Method static property class object referenced by region
  • The method of constant reference object region
  • JNI native method stack references (i.e., the process generally Native) object

Four kinds of reference types

After a reference to JDK1.2 was expanded into strong references, soft references, weak references, phantom references four kinds, these four strength once weakened. By reference to the expansion, it can be described in terms of memory usage such objects: when sufficient memory is retained in memory; if the memory space for garbage collection is still very tight, you can discard this type of object. Many system caching features correspond to such a scenario.

  • Strong reference
    in Java, the most common is the strong reference, the object is assigned a reference to a variable, the reference variable is a strong reference. When an object is strong reference variable references, it is up to the state, it is unlikely to be recycled garbage collection mechanism, even if the object in the future will never be used JVM will not be collected. So strong reference is one of the main causes Java memory leaks.

  • Soft references

    Soft references need SoftReference class to implement, subject only to the soft references, it will not be recovered when the system memory is sufficient, when there is insufficient system memory space it will be recycled. Soft references are usually used for memory-intensive programs.

  • Weak references

    Weak references needed WeakReference class to implement, it refers to the lifetime of softer than shorter, only weak references to an object, as long as a garbage collection operation, regardless of the JVM memory is not enough, there is always the recovery of the object occupy memory.

  • False quote

    PhantomReference class virtual reference needed to achieve, it can not be used alone, and the reference queue must be used in combination. The main role of the virtual reference is to track the object is garbage collected state.

Even when recovery

A realization of serial GC HotSpot VM view of the trigger conditions are divided into the following categories:

  • young GC: When young gen area of ​​eden full allocation trigger. Note that some of the young GC live objects will be promoted to the old gen, so the young GC old footprint gen typically will be increased.
  • full GC: When you are ready to be triggered once young GC, if it is found before the statistics say young average size of GC promotion of the current large surplus space than the old gen will not trigger GC young but turn trigger full GC (because HotSpot VM by GC, the concurrent collection CMS addition, the other to collect old gen are collected simultaneously by GC GC entire stack, including young gen, it is not necessary to trigger a separate advance Young GC); or, if it has perm gen, to allocate space in the perm gen but there is not enough space, but also trigger a full GC; or System.gc (), heap dump with GC, the default is triggered full GC.

HotSpot VM other non-concurrent GC in the complex trigger conditions in some, but the general principle and in fact says the same. Concurrent GC will trigger conditions are not the same. To CMS GC, for example, it is mainly to check the timing of the use of old gen, when the ratio exceeds the trigger will start a CMS GC, concurrent collection of old gen to do.

How to recycle

How to recycle mainly to relate to the garbage collection algorithm. Here are a few ideas garbage collection algorithms.

Method clear mark (Mark-Sweep)

Clear labeling algorithm is the ideological foundation of modern garbage collection algorithm. It is divided into two phases: phase marking and clearance phase. In the mark phase, first by the root, marking all objects reachable teams starting from the root, garbage objects and therefore not marked object is not referenced. Then during the cleanup phase, remove all objects are not marked.

Lack of clear labeling algorithm are: a large number of discrete memory fragmentation problems arising after efficiency and clear labeling. And too much may lead to memory fragmentation in the allocation of large objects, unable to find contiguous memory and had to trigger another garbage collection in advance.

Replication algorithm (Coping)

The core idea is to copy algorithm: the original memory space is divided into two, each only surviving copy in memory in which an object is, when garbage collection, the memory is being used to block unused, after being cleared All objects used in the memory blocks, the two memory switch roles, garbage collection is completed.

If the object to be recovered in many systems, the algorithm needs to be replicated copy live objects will be relatively small, real time garbage collection, replication algorithm efficiency will be high. And the object is in the process of garbage collection, unified copied to the new memory space, and then clear the memory of the original use, so you can ensure the recovery of the memory space is no debris. But on the other hand, the cost of copying algorithm is required to use more memory space.

Replication algorithm is more suitable for the new generation. Because the new generation of unnecessary garbage objects usually live objects, efficient replication algorithm will be higher.

Tags to organize algorithm (Mark Compact)

In the old era, most of the objects are live objects. If you still use the copy algorithm, due to the survival of the subject and more, also will increase the cost of copying. Therefore, garbage collection based on the characteristics of old age, require the use of other algorithms. Tags to organize the recovery algorithm is an algorithm years old. It made some optimization algorithm on the basis of marks. And clear labeling algorithm, as it is also more nodes from the start, but it is not clear the object is not marked, but the survival of an object to the side of memory compression, after clearing all the space outside the boundary. This method avoids the generation of debris, but also does not require too much memory space, and therefore relatively high cost.

The final effect is equivalent to the arranging method of marking flag cleared after execution of the algorithm is completed, and then finishing a memory fragmentation, and therefore it may be referred to as a clear finishing marker (MarkSweepComact).

Generational algorithm (Generational Collecting)

Generational algorithm is based on a few different objects into the memory alive cycle. The Java heap is generally divided into the old and the new generation's, so that you can collect the most appropriate algorithm according to the characteristics of each era. The new generation is characterized by an object toward evening students dead, about 90% of new objects will be recovered, and therefore suitable for the new generation of replication algorithm. When an object is recovered after a few are still alive, the object will be placed in memory of old age. In the old era can be considered an object over time, even throughout the life cycle of the program, is the permanent memory, you can use tags to organize and clear labeling algorithm years old.

For years the new generation and the old, the new generation of recovery is usually very high frequency, but each time is very short recovery time-consuming, and recycling old's relatively low frequency, but will consume more time.

Partition algorithm (Region)

In general, under the same conditions, the greater the heap, the longer the time required for GC events, so that the longer the pauses generated. In order to better dwell time by the will of a large memory area is divided into small areas just like multiple sizes, according pause time goals, each recovered several small intervals, rather than the entire heap space, thereby reducing the time GC pause generated. Partitioning algorithm is divided into a whole heap space between successive different cells. Independently each inter-cell independent recovery.

Guess you like

Origin www.cnblogs.com/fzsyw/p/11409432.html