Java garbage collection mechanism---generation algorithm

There are many garbage collection algorithms in jvm. This article mainly introduces the generation algorithm.

1. Principle

First of all, the life cycle of each object is different, so the generation algorithm adopts different recycling strategies for different objects.

Young generation: The young generation is set up to quickly clean up those objects with a short life cycle. The young generation is divided into three modules, an eden area, and two survivor areas (survivor0 and survivor1). Their memory is 8:1:1 For allocation, a new object is first created in the eden area. The recovery of the young generation is called minor GC. When reclaiming, the surviving objects in the eden area are copied to the survivor0 area, and the eden area is then cleared. When the memory in the survivor0 area is full, the eden area and the survivor0 area store all the surviving objects in the survivor1 area. Then clear the current survivor0 area and the eden area, and then exchange the current survivor1 area with the survivor0 area. Surviving objects are put into the old generation.


Old generation: After the object has experienced many experiences in the young generation, it finally survives to the old area. Therefore, most of the objects in the old generation are objects with a longer life cycle, and the memory allocated to the old generation is also higher than that of the young generation. To be large, the default is 1:2. When the memory of the old generation is full, a full GC or major GC will be triggered, which is to recycle both the young generation and the old generation.


Persistent generation: used to store static files, such as Java classes, methods, etc. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, etc. In this case, a relatively large persistent generation space needs to be set to store these newly added classes during operation.


GC execution mechanism

Since the objects are generationally processed, the garbage collection area and time are also different. There are two types of GC: Scavenge GC and Full GC.

Scavenge GC

Under normal circumstances, when a new object is generated and the space application in Eden fails, the Scavenge GC will be triggered, the Eden area will be GCed, the non-surviving objects will be cleared, and the surviving objects will be moved to the Survivor area. Then organize the two areas of the Survivor. GC in this way is performed on the Eden area of ​​the young generation and will not affect the old generation. Because most objects start from the Eden area, and the Eden area will not be allocated very large, the GC in the Eden area will be performed frequently. Therefore, it is generally necessary to use a fast and efficient algorithm here, so that Eden can be idle as soon as possible.

Full GC

tidy up the entire heap, including Young, Tenured, and Perm. Full GC is slower than Scavenge GC because it needs to recycle the entire pair, so the number of Full GCs should be minimized. In the process of tuning the JVM, a large part of the work is the adjustment of FullGC. The following reasons may cause Full GC:

  1. The Tenured is full

  2. The persistent generation (Perm) is full

  3.System.gc() is called explicitly

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


GC collector:

Collectors used by the new generation collectors: Serial, PraNew, Parallel Scavenge

Collectors used by old collectors: Serial Old, Parallel Old, CMS

Serial collector (copy algorithm)

  The new generation single-threaded collector, marking and cleaning are single-threaded, the advantage is simple and efficient.

Serial Old collector (mark-collate algorithm)

  The old generation single-threaded collector, the old generation version of the Serial collector.

ParNew collector (stop-copy algorithm) 

  The new generation collector, which can be considered as a multi-threaded version of the Serial collector, has better performance than Serial in a multi-core CPU environment.

Parallel Scavenge collector (stop-copy algorithm)

  Parallel collector, pursuing high throughput and efficient use of CPU. Throughput is generally 99%, throughput = user thread time / (user thread time + GC thread time). It is suitable for scenarios that do not have high requirements for interaction, such as background applications.

Parallel Old collector (stop-copy algorithm)

  Parallel Scavenge收集器的老年代版本,并行收集器,吞吐量优先

CMS(Concurrent Mark Sweep)收集器(标记-清理算法)

  高并发、低停顿,追求最短GC回收停顿时间,cpu占用比较高,响应时间快,停顿时间短,多核cpu 追求高响应时间的选择




Guess you like

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