Talk about GC recovery mechanism and algorithm

- Mainly for heap memory recovery

1.  What GC recovery algorithms are there?

 

Three questions

² Which objects can be recycled?

² How to recycle?

² When will it be recycled?

 

Ø  Solve which objects can be recycled?

1)  Reference counters       are older algorithms

Principle: If the object is referenced once, mark +1; reduce a reference, mark -1; if it is not used for a long time, it will be recycled when the mark is 0

Problem: It is difficult to solve the problem of circular reference of objects, that is, two objects refer to each other and cannot be cleared consistently

 

  2) Commonly used algorithms for reachability analysis algorithms

Principle: related to GCRoot, keep it; no relationship to clear; generally only stack objects, static variable objects, and constants can be used as GCRoot.

 

Ø  How to recycle?

1)  Mark-Clear

Question: A large number of discontinuous memory fragments will be generated, and large files cannot be saved. What should I do? After collecting it once, it still cannot be saved, what should I do?

Virtual memory mechanism, saves some files to disk IO; but not used, affects performance

 

2)  Copying algorithm

The heap memory is divided into two parts, one block is in the non-clearable state, and the other is in the clearable state, in a 1:1 relationship, collecting the new generation in the memory

Problem: Low memory usage

Improvement: 1 large Eden and two smaller Suivior spaces, the newly created ones are placed in Eden;

When clearing, the clearing can be cleared, and the ones that cannot be cleared are placed in Suivivor, (8:1) mainstream use;

The improvement of Cpoying algorithm improves the efficiency of memory management.

The new generation cannot survive, it often survives, and part of the data is stored in the old generation.

 

3) Marking-collating algorithm

GC rarely occurs in the old generation, and they are all frequently used objects; they are used in the old generation.

After the memory is full, move some unused tags to a block, and delete them without causing memory fragmentation

 

4) Generational collection algorithm:

Divide the entire heap into the new generation, and the old generation,

 

 

Ø  Who will recycle?

garbage collector to recycle garbage

What garbage collectors are there?

 

--Cenozoic   

copying algorithm

All generations have been innovating, but not completely replacing

 

The first generation Serial:

Jdk1.3 version, single-threaded collector, all other threads will stop and wait for it to complete the work

This phenomenon cannot be eliminated, and the follow-up is to continuously optimize the process of reducing time

 

It's good to use it when the memory footprint is small, and it can improve efficiency.

 

Second generation: ParNew

Multithreaded version of Serial

Concurrent collector, garbage thread and user thread work at the same time

3rd Generation: Parallel Scavenge Collector

A controllable throughput can be achieved, with the throughput prioritizing the collector

Programs that frequently interact with users do not use

 

Spark uses this collector by default. You can adjust spark-defaults.conf in this configuration file, and the collectors can be combined

 

 

- old age

Parallel Old,

 

CMS collector

A collector with the goal of obtaining the shortest recovery pause time, implemented in the "mark-sweep" algorithm

There is a concurrent pause collector called

 

The initial mark only marks the objects that GCroot can associate with, the speed is very fast, and the user thread will stop

Concurrent marking Concurrent marking phase, user thread will not be stalled

Remark to correct changes made in the concurrent phase

Concurrent clearing user thread and garbage thread work at the same time, user thread will not stop

 

It is recommended to use in many user interaction scenarios.

 

There is a problem: it is very sensitive to the resources of the Cpu and cannot handle floating garbage

 

Therefore, it is necessary to set aside some space for floating garbage, which is less than 10% by default.

Will generate a lot of memory fragmentation.

 

 

 

 

 

Ø  GC calculation!

In traditional JVM memory management, we divide the Heap space into two partitions, Young and Old, and the Young partition includes one Eden and two Survivor partitions, as shown in the following figure. The newly generated objects will be stored in the Eden area first, and each time a minor GC occurs, the JVM copies the surviving objects in the Eden partition to an empty Survivor partition on the one hand, and another Survivor partition that is being used on the other hand. The surviving objects in are also copied to the empty Survivor partition.

 

During this process, the JVM always keeps a Survivor partition in a completely empty state. After an object is copied between two Survivors for a certain number of times, if it is still alive, it is copied into the Old partition. When the Old partition does not have enough space, the GC will stop all program threads and perform a Full GC, that is, sorting out the objects in the Old area. Note: During Full GC, all threads are paused, so this phase is called Stop-The-World (STW), and it is also the part that has the greatest impact on performance in most GC algorithms.

2.  Sparc memory management GI recycling algorithm?

The G1 GC completely changed this traditional thinking. It divides the entire Heap into several pre-set small area blocks, and no new and old partitions are made inside each area block, but the entire area block is marked as Eden/Survivor/Old. When a new object is created, it is first stored in an available block (Region). When the block is full, the JVM will create a new block to store the object. When a minor GC occurs, the JVM copies the surviving objects in one or several blocks to a new block, and selects several brand-new blocks in the free space as new Eden partitions. Full GC occurs when there are live objects in all regions and no empty blocks can be found. When marking surviving objects, G1 uses the concept of RememberSet to record the references outside each partition to the inside of the partition in the RememberSet of the partition, which avoids scanning the entire Heap and makes the GC of each partition more independent.

 

In this context, we can see that G1 GC greatly improves the heap occupancy rate when Full GC is triggered, and also makes the pause time of Minor GC more controllable, which is very friendly to environments with large memory

              

For the complete list of GC parameters supported by the Hotspot JVM, you can refer to the explanation of some parameters in Oracle's official documentation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325113010&siteId=291194637