Java Virtual Machine [2]

Garbage collector and memory allocation strategy

Program counter, stack virtual machine, native method stacks three regions with thread birth and death, in the stack frame of the stack as the method of entry / exit, and the allocated memory size is determined after the class structure is known, these memory areas allocation of recovered OK. CG is a need to consider the Java heap and method area, different one of the plurality of interface implementation class required memory, the method requires a plurality of memory branches are different. So this is part of the garbage collector interest.

1. Make sure the object "life and death"

Recycling garbage collector "dead" objects, object determination is required "life and death" Method: 1. 2. Reference counting reachability analysis algorithms

1.1. Reference counting

Add a reference to an object counter, where there is a reference to it, counts +1, -1 fail count reference count for the object 0 can not be used again.

Disadvantage: it is difficult to solve the problem (as an example) of the reference target loop

 

public  class ReferenceCountingGc {
     public Object instance = null ;
     Private  static  Final  int _iMB = 1024 * 1024 ; 
     Private  byte [] = bigSize new new  byte [2 * _1MB]; // total memory used, as to whether the log is recovered GC viewed 
    
    public  static  void testGC () { 
        ReferenceCountingGC objA = new new ReferenceCountingGC (); 
        ReferenceCountingGC objB = new new ReferenceCountingGC (); 
        objA.instance = objB; 
        objB.instance = objA; 
        System.gc (); // Since two objects refer to each other, the reference count is not zero and therefore, can not be recycled    
    } 
}

 

 

 

1.2. Reachability analysis algorithm

 

The basic idea is through a series of objects called "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when an object has no references to GC Roots when connected to the chain, then the proof is not available object.

In the Java language, as GCRoots objects include the following categories:

  1. Virtual Machine stack (local variables in a stack frame, also called the local variable table) referenced object.
  2. The method of the referenced object region class static properties.
  3. Object literal reference methods zone.
  4. Object native method stack JNI (Native Method) references.

If the object covering up a virtual machine or finalize method has been called, the object will escape a "death" can only be the second time from the GC reference mark and a chain of office objects associated " death".

1.3. References

The original quote is cited and referenced objects only two states, for some less "important" for the object to be too stiff, so strong references by adding, soft references, weak references, phantom references method allows sufficient memory space if it remains in memory, if not enough memory after GC abandoned.

Strong reference: The Object obj = - new Object ()

Soft Quote: before memory overflow occurs, it will recover

Weak references: Object weak references only to live before the next GC process, even if memory is sufficient or not, recycling out

False quote: There is a virtual reference does not affect their survival time. Only when the object to be notified and is recovered after the collector.

1.4. Method recovery zone

The method of determining whether or not the constant "waste constant" in:

  1. All instances of the class have been recovered, Java heap class does not exist in any instance
  2. ClassLoader load is recovered
  3. Java.lang.Class class object is not referenced anywhere (including reflection)

Example: String Object "abc" into the constant pool, but there is no reference to this constant String object, the object will be recovered when garbage collection occurs

2. The garbage collection algorithm

 2.1 mark - clear algorithm

Algorithm is divided into "mark" and "clear" two stages. Mark phase will need to be recovered object tag, cleanup in the cleanup phase

Disadvantages: 1. Clear labeling and efficiency is not high. Will produce large amounts of debris after the discontinuity 2. cleared, when the need to allocate large objects, will trigger another garbage collection in advance

2.2 replication algorithm (based on 2.1) ( Common )

The capacity is divided into two equal amounts, with each one of them, when the memory runs out, there is also copied onto another one of the objects, and then has been used to clean out a memory space, each time the entire semi memory recovery zone

Advantages: high efficiency

Cons: Small memory to half of the original search, copy, low operating efficiency

Modern commercial virtual machine using this method. Since 98% of the target "dead Chaosheng Xi", so the memory space is divided into a larger and smaller Survivor Eden space. When recovered, the copy is still alive in Eden and Survivor object to another piece of Survivor, and then clear the space previously used. If Survivor is not enough, to be secured with the old era, the object directly into the old year by allocating guarantee mechanism.

2.3 mark - Collation Algorithm (based on 2.1)

Compared mark - sweep, not recyclable objects in the clearing process to clean up, but to live objects move toward one end, then clean out the memory outside of the boundary.

2.4. Generational collection algorithm

Depending on whom survived cycle, the memory is divided into a few (New Generation / Older Generation), using the appropriate algorithm according to the characteristics of each era. As much dead with copy algorithm, live more with 2.1 or 2.2

3. Memory Allocation recovery strategy

Overall, allocated on the heap, mainly distributed in the new generation of Eden, Eden district if there is not enough space, the occurrence of MinorGC (new generation GC) occurs, if not enough, the original target area to guarantee to the elderly.

Large object enters years old

Long-term survival of the object enters years old

PS: GC years old compared to more than 10 times slower Minor GC (Full GC / Major GC)

Guess you like

Origin www.cnblogs.com/tillnight1996/p/12000082.html