Explanation of GC garbage collection in java

Prerequisite understanding:

Java memory is divided into five parts:

Heap (store new objects), JVM method stack (execution method),

Local method stack (execute local method), method area (store class information, store method), program counter

=========================================================================

1: Garbage collection, first confirm which ones are garbage

There are five parts in java memory. The data in the stack is pushed out of the stack along with the method, and the program counter occupies a small amount of memory space; so there is only the heap and the method area .

Garbage needs to be resolved.

Determine whether the new object in the heap memory is garbage?

  Using reachability analysis, starting from the stack and method area, if an object is not referenced in the stack or method area, then the object is considered a garbage object.

Determine whether the class and constant in the method area are garbage?

  The instance objects have been recycled; the ClassLoader that loaded the class is recycled; the object corresponding to the class is not referenced anywhere, and the method of the class cannot be accessed through reflection anywhere, then the class is considered a garbage class.

2: Secondly, what are the garbage collection algorithms

 Using classification and recycling algorithms, including replication algorithms (suitable for a small number of objects to survive), sorting and recycling algorithms (suitable for a large number of objects to survive)

The new objects are first placed in the young generation memory. The young generation is divided into three parts: eden, survivor, and survivor. The copy algorithm is adopted; the objects are generated in the eden area. When the eden area is full, the surviving objects will be copied to the survivor. Area, when the survivor area is full, the surviving objects in the secondary area will be copied to another survivor area. If the survivor area is also full, the objects that are copied from the first survivor area and are still alive will be copied to Old age.

Sorting and recycling algorithm used in the old age

Guess you like

Origin blog.csdn.net/Growing_hacker/article/details/108204099