14, Java garbage collection

Principle and garbage collection algorithm

  Java garbage collection mechanism introduced, so that C ++ programmers the most headaches memory management problems immediately. Java programmers can put more energy into the business logic above, rather than the amount of memory on top of the tube, greatly enhance the development efficiency. This is because Java's automatic garbage collection mechanism.

Memory Management

Java memory management to a large extent refers to the object management, including space allocation and release objects.
Assigned space: using the new keyword to create an object can be sent.
To free up space like: assigning objects to null. Garbage collector reclaim the memory space will be responsible for all the "unreachable" objects.

Garbage collection process

Any garbage collection algorithms generally do two basic things.

1, found useless objects.
2, recycling useless memory space occupied by the object.

  Garbage collection mechanism to ensure that "useless object" can be recycled. Useless object refers to the object without any variable references. Java's garbage collector found useless objects through the relevant algorithm, and removal and finishing.

Garbage collection algorithms related

1, reference notation:
  heap Each object has a reference count. Is referenced once, count is incremented, the reference variable value becomes null, the count is decremented until the count is 0, then becomes useless object. The advantage is simple algorithm, the disadvantage is "useless objects with circular references" is not recognized.
Circular reference example

public class Student{
    String name;
    Student friend;
    
    public static void main(String[] args){
        Student s1 = new Student();
        Student s2 = new Student();
        
        s1.friend = s2;
        s2.friend = s1;
        
        s1 = null;
        s2 = null;
    }
}

  referenced s1 and s2 each other, causing them to reference count is not 0, but is actually useless objects, but can not be identified.
2, quoted up law
  after the program all references to the relationship seen as a map from a node GC ROOT start, find the corresponding reference node, find the node, continue to look for this node reference nodes, the nodes to find when all references after completion, the remaining nodes is considered not to be a reference to the node, the node that is useless.

Common generational garbage collection mechanism

Generational garbage collection mechanism is based on the fact: life different objects of weeks Sri Lanka are not the same. Therefore, objects of different life cycle can take different recovery algorithms, it can improve the recovery efficiency. We were divided into three states: the young generation, old generation, lasting generations. The JVM heap memory is divided into (Eden, Survivor) (the young generation) and (Tenured / Old) (old generation) space.

Young state

First of all newly created objects are placed in Eden district, the young generation's goal is to quickly collect as much as possible out those with short life cycles like, corresponding to the Minor GC, every Minor GC will clean up the young generation of memory, algorithm uses high efficiency replication algorithm, frequent operations, but will be a waste of memory space. When the "younger generation" storage area full of objects, the object will be stored in the old generation area.

Old generation

After the younger generation by Li 15 times garbage collection, objects are still alive, will be placed in the old generation, because some may think the old generation are stored in some of the longer life cycle of the object. More and more old generation object, we will start the Major GC and Full GC (total amount recovered), to a clean, comprehensive clean-up the young generation and the old generation regional area.

Endurance fee

Generation of files used to store persistent state, such as Java classes, methods, lasting no significant effect on the generation of garbage collection. There is a permanent generation of objects stored inside the method area.

Minor GC

Used to clean up the young in the area, Eden area is full it will trigger a Minor GC, clean up useless objects, copy live objects to Survivor1, Survivor2 area, these two regions, the size of the space is the same, that only one in use, a Is empty.

Major GC

Used to clean up old's region.

Full GC

Used to clean up the young generation, old generation. Soft high costs, have an impact on system performance.

Garbage collection process

1, create a new right, the majority of which are stored in Eden.
2, when Eden is full, you can not create a new object, triggering garbage collection GC, will clean out useless objects, useful objects to copy Survivor, such as: S1 while cleanup Eden area.
3, when Eden area again full, S1, will not be emptied into another storage Survivor objects, such as: S2, while the Eden area of the object can not be emptied, also copied to S2, and ensure Eden S1, are cleared.
4, repeated many times (default 15) after Survivor is not an object to be cleaned, it will be copied to the old's Old area.
5, when the Old district is full, it will trigger a once complete garbage collection (Full GC).

Full GC JVM tuning and

In the process of tuning the JVM, a large part of the work is adjusted to the Full GC. Because when Full GC run, very affect performance, so we do not call as much as possible to the Full GC.

The following reasons may lead to Full GC:

1, the old generation (Tenured) is filled.
2, the permanent generation (Perm) is filled.
3, System.gc () is called.
4, the last time the dynamic allocation strategy changes each domain Heap after GC.





Details determine success or failure!

Personal humble opinion, if not, ask righting!

Guess you like

Origin www.cnblogs.com/xdtg/p/12359758.html