jvm knowledge point - garbage collection method

What are the methods of garbage collection in java?

 1. Mark-clean

This garbage collection method can be known from the name. His idea is to mark those objects to be recycled, and then recycle and clean up uniformly. This method is simple, but there are two main problems:

(1) The efficiency is not high, and the efficiency of marking and clearing is low. (2) A large number of memory fragments will be generated, resulting in insufficient memory to trigger a GC action in advance when the program allocates large objects.



 

2. Replication algorithm

 In order to solve the problem of efficiency, the copy algorithm divides the available memory into two equal parts by capacity, and then uses one of them each time. When a block of memory is used up, the surviving objects are copied to the second block of memory, and then

Clear the first block of memory at one time, and then copy the objects of the second block to the first block, but this method, the cost of memory is too high, and the general memory is basically wasted every time.

Therefore, the algorithm was improved. The memory area is no longer divided by 1:1, but the memory is divided into three parts of 8:1:1. The larger memory is handed over to the Eden area, and the remaining two smaller memory areas. It is called the Survior area, and the Eden area will be used first every time.

If the Eden area is full, copy the objects to the second memory, and then clear the Eden area. If there are too many surviving objects at this time, so that the Survivor is not enough, these objects will be copied to the old age through the allocation guarantee mechanism.



 

3. Mark-Organize

This algorithm is mainly to solve the problem of mark-sweep, which generates a large number of memory fragments; when the object survival rate is high, it also solves the efficiency problem of the replication algorithm.

The difference is that when the object is cleaned up, the surviving object is moved to one end, and then the objects outside the boundary are cleaned up, so that there will be no memory fragmentation.



 

4. Generational collection

Nowadays, most of the garbage collection of virtual machines adopts this method, which divides the heap into the new generation and the old generation according to the life cycle of the object. In the new generation, due to the short life cycle of objects, a large number of objects die each time they are recycled, so at this time

The replication algorithm is used. Objects in the old generation have a higher survival rate and have no additional space for allocation guarantees, so use mark-sort or mark-sweep

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326834392&siteId=291194637