JVM Garbage Collection-Garbage Collector

JVM Garbage Collection-Garbage Collector

serial garbage collector

SerialSerial: Designed for a single-threaded environment and only uses one thread for garbage collection. It will suspend all user threads, so it is not suitable for server environments. It is suitable for personal computers with small heap memory.

Enable serial garbage collection

-XX:+UseSerialGC = Serial + SerialOld
  • Serial: Enable new generation serial garbage collection, using replication algorithm
  • SerialOld: Enable serial garbage collection in the old generation and use the mark sorting algorithm.

Insert image description here

  • First, let the user threads reach a safe point [memory address changes are involved during garbage collection]. All user threads stop running. The garbage collection thread collects the garbage. After the garbage collection is completed, the user threads resume running.
Throughput-first garbage collector
  • Multithreading

  • The heap memory is large and requires multi-core CPU support (otherwise, multiple threads compete for the same CPU time segment)

  • Within unit time, stw has the shortest time

    0.2 0.2=0.4 一小时内总的stw时间最短,单次较长
    

Insert image description here

Enable throughput-first garbage collector

-XX:+UseParallelGC ~ -XX:+UseParallelOldGC //开启一个,另一个也会自动开启(jdk1.8默认开启)
  • +UseParallelGC: Enable new generation throughput priority garbage collection, using replication algorithm

  • +UseParallelOldGC: Enable old generation throughput priority garbage collection, using mark sorting algorithm

  • -XX:ParallelGCThreads=n Set the number of garbage collection threads. If not set, the default number of garbage collection threads will be consistent with the number of CPU cores.

  • -XX:+UseAdaptiveSizePolicy automatically calculates the sizes of Eden, From and To areas according to the GC situation. The promotion threshold will also be affected. It is enabled by default in jdk1.8

  • -XX:GCTimeRatio=ratio GC time ratio, ratiothe default is 99, calculation formula: gc time ratio=1/(1+ratio), that is, the default ratio is 1/100, which is equivalent to 100 minutes of garbage collection time no greater than 1 minute. If it is greater than 1 minute, the heap memory size will be automatically adjusted and the heap memory will be increased.

    heap size++ -> gc count--
    
  • -XX:MaxGCPauseMillis=ms Maximum pause time for garbage collection

    • The default is 200ms. When the heap memory increases, the time required for each gc will also increase (the garbage in the heap needs to be scanned, and the scanning and recycling time will increase), so the need to ensure the maximum pause time means that the heap will decrease. Obviously , which GCTimeRatioconflicts with
Response time priority garbage collector
  • Multithreading

  • Large heap memory, multi-core CPU scenario

  • stwKeep the time of each trip as short as possible

    0.1 0.1 0.1 0.1 0.1 =0.5  单次时间最短 总时间较长
    

Insert image description here

  • Enable response time priority garbage collector

    -XX:+UseConcMarkSweepGC ~ -XX:+UseParNewGC ~ SerialOld
    
    • UseConcMarkSweepGC (CMS): A garbage collector based on mark and sweep, and it is concurrent and works in the old generation.
    • UseParNewGC: A garbage collector that works in the new generation and uses a copy algorithm
    • When CMSthe garbage collector fails concurrently concurrent mode fialure, it will degenerate into SerialOlda garbage collector.
    • -XX:ParallelGCThreads=n(Number of parallel threads for garbage collection) ~ -XX:ConcGCThreads=threads(Number of concurrent threads for garbage collection), the number of concurrent threads is generally set to 1/4 of the number of parallel threads
    • -XX:CMSInitiatingOccupancyFraction=percentThe memory ratio when performing cms garbage collection. Assume percent=80, that is, when the old generation memory usage reaches 80%, a memory cleanup is triggered (because it is concurrent cleanup, new garbage [floating garbage] will be generated during the cleanup, and space needs to be reserved. occupied by this part of the garbage)
    • -XX:+CMSScavengeBeforeRemarkUsing Young GC before the remark phase of CMS GC starts ParNewGCwill help reduce the invalid references of Young Gen to Old Gen and reduce the time overhead of CMS-remark phase.
There is a problem
  • The CMS garbage collector uses a mark-and-sweep algorithm, so memory fragmentation will occur.
  • 80% of CMS's GC time is spent in the remark phase, and the pause time in the remark phase will be very long.
  • concurrent mode failure: This exception occurs when cms is being recycled. During the execution of CMS GC, the business thread is also running at the same time. When the young generation space is full, ygcsurviving objects need to be put into the old generation during execution. At this time, the old generation space is insufficient, and CMS has not had a chance to recycle it. It is generated in the old generation, or when doing Minor GC, the new generation rescue space cannot be accommodated, so it needs to be placed in the old generation, and the old generation cannot be accommodated.
  • Promotion failed: When performing Minor GC, the Survivor space is insufficient, and the object can only be placed in the old generation. At this time, the old generation cannot be placed. Mostly because the old generation has enough free space, but due to many fragments, the new generation cannot be placed. The object to be transferred to the old age zone is relatively large, and a continuous area cannot be found to store the object. The next promotion failedstep will occur concurrent mode fialure, which will degrade the garbage collector SerialOld. At this time, the throughput will drop seriously.
Four stages of CMS garbage collector**
  • Phase 1 (initial marking): Mark all root objects in the old generation, including objects directly referenced by the root object, and old generation objects referenced by all surviving objects in the young generation (just mark those that GC Roots can directly relate to) object, very fast), will triggerstw

  • Phase 2 (concurrent marking): Find all surviving objects starting from the objects marked in the initial marking phase

    Because it runs concurrently, during operation, objects in the new generation will be promoted to the old generation, or objects will be allocated directly in the old generation, or the reference relationships of objects in the old generation will be updated, etc. These objects need to be remarked. Otherwise, some objects will be missed and missed bids will occur. In order to improve the efficiency of re-marking, this stage will mark the Card where the above-mentioned objects are located as Dirty. Subsequently, only the objects of these Dirty Cards need to be scanned to avoid scanning the entire old generation; the concurrent marking stage is only responsible for marking Cards whose references have changed as Dirty. status, not responsible for processing

  • Phase 3 (re-marking): In order to correct the marking records of that part of the objects that have changed due to the continued operation of the user program during the concurrent marking period, the pause time in this stage is generally slightly longer than the initial marking stage, but much longer than the concurrent marking period. The time is short. This stage also requiresstw

  • Phase four (concurrent clearing): This phase mainly clears unmarked objects and reclaims space)

Guess you like

Origin blog.csdn.net/qq_52751442/article/details/132211688