Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the JVM garbage collection algorithm and garbage collector?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Garbage collection algorithm replication algorithm (Copying) marking-clearing algorithm (Mark-Sweep) marking-finishing algorithm (Mark-Compact)

Replication algorithm:

Advantages, high efficiency, no memory fragmentation

The disadvantage is that the utilization rate of memory is low, which can only be used by half, and the efficiency decreases obviously when there are more surviving objects.

Eden from to 8 from 1 most objects in java die overnight, so only 10% of the objects need to be recycled, 10% (to) + 10% (reserved)

Tag removal algorithm:

Pros: 100% space utilization

Disadvantages: marking and clearing are not efficient, resulting in a large number of discontinuous memory fragments

Process: 1. First mark all the objects that need to be recycled 2. Uniformly reclaim tagged objects

Tag collation algorithm:

Advantages: 100% utilization, no memory fragmentation

Disadvantages: the efficiency of marking and clearing is not high, and the efficiency is lower than that of marking-clearing.

Process: instead of cleaning directly after marking, the surviving object is moved to one end, and then the memory outside the boundary of the end is cleaned.

Garbage collector

Generation algorithm: choose no garbage collection algorithm according to the characteristics of each age

New generation: using replication algorithm

Old age: using the mark-organize or mark-clear algorithm

Parallel: multithreading of garbage collection at the same time

Concurrency: multithreading for garbage collection and multithreading for applications simultaneously

Recycler

1According Seralcontrol serial Old: single thread

2Parnew: basically no different from Seral, multithreaded

3Query parallel Scavenge (ParallerGC) / Parallel Old: a collector focused on throughput, mainly used for background computing tasks that do not require much interaction.

4CMS: a collector with the shortest pause time. Use the tag cleanup algorithm

5Magne G1G1Rank jdk1.9 later recommended for use, spanning the new and old generations. Use tags to organize and break up into pieces

All the new generations use replication algorithms, old-age tag collation and tag removal.

Concurrent MarkSweep (CMS):

-XX:+UseConcMarkSweepGC, applied to Bamp S system server, pays attention to the response speed of the service, and hopes that the system will have the shortest pause time and give users a better experience.

Garbage collection process:

Initial tagging: marking only objects directly related to GC Roots, which is fast and requires pausing concurrent tagging: doing reachability analysis from GC Roots objects, finding surviving objects takes the longest time, and does not need to pause re-tagging: during the modification of concurrent tagging, the part of markup changes caused by the continued operation of the program requires a STW, which will be longer than the initial tagging. But shorter than concurrent marking time and concurrent cleanup: no pause is required

Pros: the longest concurrency markup and concurrency cleanup can work with user threads.

Disadvantages:

1CPU resource sensitivity: concurrent temporary CPU resources, lack of CPU, and obvious decrease in efficiency.

2. Floating garbage: in the concurrent cleaning phase, due to the garbage generated by the user thread continuing to run, CMS cannot deal with it and needs to wait for the next GC processing. This part becomes "floating garbage".

3, space debris will be generated: the mark-cleaning algorithm will produce discontinuous space debris G1 garbage collector

-XX:+UseG1GC

Internal layout change

G1 divides the heap into several independent regions of equal size (Region), and the Cenozoic era is no longer physically separated from the old age.

Algorithms: tag-collation (humongous) and copy recovery algorithm (survivor).

GC mode

Young GC (Recycling Eden, Survivor Zone): select Region of all younger generations, and control the time cost of young GC by controlling the number of young generations (copy recovery algorithm)

Mixed GC (full area Recycling): not only the younger generation of Region but also a number of Region with high income in the old years are selected for release. Mixed GC is not Full GC, and if the Mixed GC fails to keep pace with the memory allocated by the program, it uses serial odl GC (Full GC) to collect the entire GC heap. 4G1 is not Full GC.

Global concurrent marking

1. Initial tagging: just mark the objects that GC Roots can directly associate with. This phase requires a STW, but it takes a short time.

2, concurrent marking: starting from GC Roots to analyze the reachability of the heap and find live objects, which takes a long time, but in parallel with the user program.

3, final tag: in order to correct the part of the tag record that changes the tag due to the continuation of the user program in the concurrent marking phase. Threads need to be paused (STW), but can be executed in parallel.

4, filter recovery: first, sort the recovery value and cost in each Region, and specify the recovery plan according to the GC pause time expected by the user. This phase is executed in parallel with the user, but because part of the Region is reclaimed, the time is user-controllable, and pausing the user thread can significantly submit collection efficiency.

Characteristics

1, space demarcation: no memory fragmentation. The algorithm is mark-collation and copy recovery algorithm.

2, predictable pause: G1, brushing is not often used, but brushing those areas with higher recovery efficiency (higher recovery return ratio). A total of 1000 areas, one area 10m * screening the commonly used areas are in urgent need of recycling.

Stop The World

The goal of the GC collector and GC optimization is to minimize the time and frequency of STW.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report