CMS algorithm

I. Introduction

The java heap Heap is divided into two parts: the new generation and the old generation. The garbage collection algorithm is mainly aimed at these two memory areas.

1. Accessibility Analysis

Recursively search for the referenced objects from the GC Root. If an object is not in this reference chain, then the object is unreachable and should be recycled.

Objects that can be used as GC Roots:

  1. Objects referenced by class static properties in the method area - stored in the runtime constant pool;
  2. The object referenced by the constant in the method area - the final modified attribute;
  3. Objects referenced in the local variable table in the stack frame of the virtual machine stack - the virtual machine stack is unique to the thread.
2. CMS applicable scenarios

CMS (concurrent mark sweep) is for applications that prefer short garbage collection pause times and are willing to share cup resources with the garbage collector during runtime.

Typically, if an application runs on a multi-core processor, and its old age has more long-lived data, then it is more suitable to use CMS. CMS can be XX:+UseConcMarkSweepGCenabled with parameters.

3. Excessive GC time and memory overflow

CMS will throw OutOfMemoryErrorif too much time is spent in garbage collection: if more than 98% of the time is spent in garbage collection and less than 2% of the heap memory is reclaimed, then OutOfMemoryErrorit will be thrown.

This feature is designed to protect the application from running for a long time, while the heap memory is too small to make garbage collection progress. If desired, this feature can be disabled with the parameter: -XX:UseGCOverheadLimit.

2. CMS execution cycle

The CMS execution cycle is divided into 6 stages, namely: 1.初始标记->2.并发标记->3.并发预清理->4.重新标记->5.并发清理->6.重置. Among them, 1 and 4 initial marking and re-marking phases all need to suspend all user threads.

2.1 Initial marking

This phase suspends all user threads and marks objects directly associated with the GC Root .

2.2 Concurrent marking

GC Root tracing is performed in this phase: all suspended threads in the initial marking phase start to run; starting from the previously marked objects, mark the objects indirectly related to the GC Root.

2.3 Concurrent Precleaning

This phase is used to mark objects promoted from the young generation to the old generation, newly allocated large objects to the old generation, and objects that have been modified during the concurrent marking phase.

2.4 Relabeling

Suspend all application threads, rescan objects in the heap (new generation, old generation), perform reachability analysis, and mark objects. This stage is multi-threaded, and with the previous foundation, it will be very fast.

2.5 Concurrent Cleanup

This phase cleans up invalid objects .

2.6 Reset

CMS clears internal state in preparation for the next recycling.

Guess you like

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