Boring JavaEE from entry to abandonment (four) garbage collection mechanism (Garbage Collection)

table of Contents

1. The principle and algorithm of garbage collection

1. Memory management

2. Garbage collection process

3. Garbage collection related algorithms

2. Universal generational garbage collection mechanism

1. The young generation

2. Older generation

3. Permanent generation

4. Three GCs

Three. JVM tuning and Full GC

Four. Other points

Five. Operations that are likely to cause memory leaks in development_Summary of garbage collection knowledge

1. Create a lot of useless objects

2. The use of static collections

3. Various connection objects (I0 stream object, database connection object, network connection object) are not closed

4. Use of Listener


Java introduced a garbage collection mechanism, which solved the most troublesome memory management problem of C++ programs. Java programmers can put more energy on business logic instead of memory management, which greatly improves development efficiency.

1. The garbage collection process is generally divided into two steps. Which two steps are they?

Answer: (1) How to find garbage (2) Recycling
2. What are the two common algorithms for garbage collection?

Answer: Reference counting method, reference reachable method (root search algorithm)
3. Heap memory is divided into: young generation, old generation, permanent generation. The garbage collector is divided into: Minor GC, Major GC, FulIGC. Which areas do these three garbage collectors correspond to?

Answer: Minor GC corresponds to the young generation, Major GC corresponds to the old generation, and FulIGC corresponds to all.
4. In the process of tuning the JVM, a large part of the work is the adjustment of Full GC. is this sentence correct?

answer.
5. What is the role of System.gc()?

Answer: It is recommended to start the garbage collection thread.

 

1. The principle and algorithm of garbage collection

1. Memory management

Java's memory management refers to a large extent: the management of objects in the heap , including the allocation and release of object space.
The allocation of object space: use the new keyword to create an object.
Release of object space: Assign the object null. The garbage collector will be responsible for reclaiming the memory space of all "unreachable" objects.

2. Garbage collection process

Any kind of garbage collection algorithm generally has to do two basic things:
1. Find useless objects
2. Reclaim the memory space occupied by useless objects.

The garbage collection mechanism guarantees that "useless objects" can be recycled. A useless object means that no variable refers to the object. Java's garbage collector finds useless objects through related algorithms, cleans them up, and organizes them.

3. Garbage collection related algorithms

1.
Each object in the reference counting method heap corresponds to a reference counter. When a reference points to this object, the reference counter is incremented by 1, and when the reference to the object becomes invalid (the reference becomes null), the reference counter is decremented by 1 , Finally, if the value of the object's reference calculator is 0, the Java garbage collector will consider the object to be a useless object and recycle it. The advantage is that the algorithm is simple, but the disadvantage is that "circularly referenced useless objects" cannot be identified.

2. The reference reachability method (root search algorithm)
program treats all reference relationships as a graph, starting from a node GC ROOT, looking for the corresponding reference node, after finding this node, continue to look for the reference node of this node, when After all the referenced nodes are searched, the remaining nodes are considered as unreferenced nodes, that is, useless nodes.

2. Universal generational garbage collection mechanism

The generational garbage collection mechanism is based on the fact that the life cycle of different objects is different. Therefore,
objects of different life cycles can adopt different recycling algorithms to improve recycling efficiency. We divide objects into three states: young generation, old generation, and permanent generation. At the same time, put objects in different states into different areas of the heap.
JVM divides the heap memory into Eden, Survivor and Tenured/Old space. (Eden, Survivor belong to the young generation, Tenured/Old belong to the old generation)

1. The young generation

All newly generated objects are first placed in the Eden area. The goal of the young generation is to collect those objects with a short life cycle as quickly as possible. This corresponds to Minor GC. Each time Minor GC will clean up the memory of the young generation. The algorithm uses a more efficient copy algorithm and frequent operations, but it will Waste memory space. When the "young generation" area is full of objects, the objects are stored in the old generation area.

2. Older generation

Objects that survive N (default 15) garbage collections in the young generation will be placed in the old generation. Therefore, it can be considered that the objects stored in the old generation are long-lived objects. There are more and more old generation objects, we need to start Major GC and Full GC (full recovery) to clean up the young generation area and the old generation area in a comprehensive way.

3. Permanent generation

Used to store static files, such as Java classes, methods, etc. The persistent generation has no significant effect on garbage collection. JDK7 used to be an implementation of "method area". After JDK8, there is no "permanent generation", use metaspace metadata space and heap instead.

4. Three GCs

Minor GC:
Used to clean up the young generation area. When the Eden area is full, a Minor GC will be triggered. Clean up useless objects and copy useful objects to the "Survivor1" and "Survivor2" areas.
Major GC:
Used to clean up the old generation area.
Full GC:
Used to clean up the young and old regions. The cost is high, which will have an impact on system performance.

Three. JVM tuning and Full GC

In the process of tuning the JVM, a large part of the work is the adjustment of Full GC. Full GC may be caused by the following reasons:
1. Tenured generation is full

2. Perm (Perm) is full

3. System.gc() is called explicitly

4. Dynamic changes in Heap's domain allocation strategy after the last GC

Four. Other points

1. The programmer has no right to call the garbage collector.
2. The programmer can call System.gc(), this method is only to notify the JVM, not to run the garbage collector. Try to use it as little as possible, you will apply to start Full GC, which is costly and affects system performance.
3. The finalize method is a method Java provides to programmers to release objects or resources, but use them as little as possible.

Five. Operations that are likely to cause memory leaks in development_Summary of garbage collection knowledge

In actual development, the system often crashes. We should pay attention to these usage scenarios for the following operations.

The scenarios that are most likely to cause memory leaks in the following four situations:

1. Create a lot of useless objects

For example, when we need a large number of concatenated strings, we use String instead of StringBuilder.

eg:

String str= "";
for (inti= 0;i < 10000; i++) {

    str += i;//相当于产生了10000个String对象
}

2. The use of static collections

The use of HashMap, Vector, List, etc. is most prone to memory leaks. The life cycle of these static variables is consistent with the application, and all objects cannot be released.

3. Various connection objects (I0 stream object, database connection object, network connection object) are not closed

Connection objects such as IO stream objects, database connection objects, and network connection objects are physical connections, and are connected to the hard disk or network. They must be closed when not in use.

4. Use of Listener

When the object was released, the corresponding listener was not deleted

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/115309941