Garbage collection mechanism and common GC algorithms in Java virtual machines

tags: Java  Development language  rear end  

1. Common GC algorithms

1. Mark-clearing algorithm

Mark the objects that need to be recycled and then clean them directly in GC.

Disadvantages: It will cause a large number of discontinuous memory fragments

2. Copy Algorithm

Divide the memory into 2 blocks, only half of it is used at a time. Each time in GC, copy the object that does not need to be recycled to another piece, and then clean up the used space at once.

Disadvantages: Only half of the memory can be used at a time, the memory utilization rate is low, and the copying process will consume performance.

3. Marking-organization algorithm

It's similar to the above, just let the object that does not need to be recycled move to one end, and then directly clear the space outside the end boundary.

4. Generation recycling algorithm

Generally speaking, Java heaps are divided into the new generation and the old generation. When the memory is insufficient, the new generation will be recycled first. After the object age reaches the threshold, it will be put into the old age. After the memory of the old age is full, the GC method of the old age will be triggered.
Generally, the new generation of GC is more frequent, and the copy algorithm can be used, and the old generation can use the mark-tidy algorithm.
The idea of ​​generational recycling is mainly to improve GC efficiency.

2. Common garbage collectors

1、Serial

Serial garbage collector, which is a single threadedNew generation garbage collector, usingCopy Algorithm, exists during garbage collection Stop The World, until the collection is complete. Recommended in Client mode.

2、ParNew

Multithreaded version of SerialNew generation garbage collector, use multi-threaded garbage recycling, adoptCopy Algorithm. Recommended in Server mode.

3、Parallel Scavenge

It is an adoptionCopy AlgorithmofNew generation garbage collector. The focus of design is to make full use of CPU resources.Reduce the time of Stop The World, focus on user experience. A variety of parameters are provided for selecting the pause time and maximum throughput.

4、Serial Old、Parallel Old

The old-age version of Serial, before jdk1.5, the garbage collection mechanism adopted was: Parallel Scavenge + Serial Old, which is the old-age version of Parallel Scavenge.

5. CMS (Concurrent Mark Sweep (concurrent mark cleaning))

CMS is based onMark-clearing algorithmofOlder garbage collector, is a garbage collector aimed at obtaining the shortest recycling pause time, and pays great attention to user experience. This achieves the (basic) work of synchronizing garbage collection threads with user threads.

Advantages: Concurrent collection, low pause.
Disadvantages: Sensitive to CPU resources, inability to handle floating garbage, mark-clearing algorithms will generate a large amount of garbage fragments.

Working process:

  1. Initial tag: Pause all other threads and record all objects directly connected to root, which is very fast.
  2. Concurrent tags: GC threads and user threads work at the same time, and use a closure structure to record reachable objects. Since the user threads will constantly update the reference domain, GC threads cannot guarantee the real-time nature of reachability analysis. This algorithm tracks and records the places where reference updates occur.
  3. Remark: Correct the tag object record updated in the concurrent tag stage, and this part will have a pause.
  4. Concurrent clearance: The user thread and the GC thread work at the same time, and the GC thread cleans up the unmarked area.

6. G1 garbage collector

G1 is aGarbage collectors for the new and old generations, new generation adoptsCopy Algorithm, adopted by the elderlyMark-organization algorithm. Based on a server-oriented garbage collector, it is mainly aimed at machines with multiple processors and large-capacity memory. It meets the GC pause time requirements with extremely high probability and also has high throughput performance.

Features: (1) Concurrency (2) Generational collection (3) Marking-organization algorithm (4) Predictable pauses

Working process: Initial marking, concurrent marking, final marking, filtering recycling.

Intelligent Recommendation

java garbage collection (GC) mechanism

Note: This article is reproduced from the following url Please indicate the source: Introduction to GC The full name of GC is Garbage Collection, which is a kind of daemon thread. In GC, garbage refer...

java GC, garbage collection mechanism

Reference article: Advantages of GC mechanism: C language requires programmers to allocate memory and free memory. This leads to the problem of memory leaks if the allocated memory is never released. ...

Java-GC-garbage collection mechanism

Java garbage collection mechanism Article Directory Java garbage collection mechanism introduction GC concept GC area GC analysis algorithm Talking about references again Heap memory recycling Method ...

Java garbage collection mechanism, GC

When it comes to garbage collection (GC), many people naturally associate it with Java. In Java, programmers don't need to care about the problems of dynamic memory allocation and garbage collection, ...

The garbage collection (GC) mechanism in java

Foreword: The garbage collection mechanism is a very important mechanism in the Java language. In Java, programmers do not need to care about the problems of dynamic memory allocation and garbage coll...

More Recommendation

JAVA virtual machine mechanism [GC] garbage collection algorithm

Part said, is to determine which objects need to be recycled herein, this ideological discussion several garbage collection algorithms (including mark - sweep algorithm, replication algorithm, mark - ...

Java Virtual Machine (JVM) garbage collection mechanism (GC) diagram

The garbage collection mechanism in Java is only in the scope heap and method area When the JVM is performing GC, it is not a unified collection of these three areas. Most of the time, the new generat...

JVM-java virtual machine, GC-garbage collection mechanism

JVM-java virtual machine The memory managed by the java virtual machine includes: java runtime data area, class sub-loading system, and execution engine. The java runtime data area has: 1. Thread shar...

Analysis of the garbage collection mechanism of Java virtual machine (GC)

content First, garbage collection mechanism (GARBAGE Collection) Second, the timing of the object recycling Reference counting method Cavity analysis algorithm Third, garbage collection algorithm Tag ...

Java GC garbage collection mechanism in-depth understanding: common garbage collection algorithm, common garbage collector, detailed collection algorithm

Garbage recovery GC Garbage Collection (Garbage Recycler) Recycling Memory GC main task: Allocate memory Ensure reference Recycle memory GC Recycling Basis: Objects No reference advantage Disadvantage...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top