Common GC garbage collection algorithms

tags: Technology Exchange  Chuanzhi Podcast Xi'an Center

Common algorithms for garbage collection
1. Reference counting method
1.1 Principle

Suppose there is an object A, any object references A, then the reference counter of object A is +1, when the reference is invalid, the reference counter of object A is -1, when the reference counter of object A is 0, it means that object A has Use quoted, then you can recycle.

1.2 Advantages and disadvantages

advantage:
  • The real-time performance is high, and there is no need to wait until the memory is insufficient before starting to recycle. At runtime, it is judged whether the object counter is 0 and recycling is performed.
  • The application does not need to be suspended during the garbage collection process. If the memory is insufficient when applying for memory, an outofmember error will be reported immediately.
  • Regional, when updating the object counter, only the object will be impressed, not all objects will be scanned.
Disadvantages
  • Every time an object is referenced, you need to update the object counter, which will increase the time overhead.
  • A waste of CPU resources, because the memory is enough, the object counter is still running statistics.
  • Unable to solve the circular reference problem (the biggest disadvantage)
Circular references such as: Object A maintains a member attribute, the type is Object B, object B maintains a member attribute, and the type is Object A, and then assign values ​​to these two member attributes separately. When assigning Object A to null, change Object B is assigned null, so that both object A and object B are null, but there is a reference relationship between a and b, so that a and b will never be recycled.

2. Mark removal method

Mark removal algorithm is to divide garbage collection into two stages, namely mark and clear
  • Mark: mark the referenced object starting from the following node
  • Cleanup: Objects that are not referenced by markup are garbage objects and can be cleaned up

2.1 Principle

This picture represents the state of all objects during the running of the program. Their flags are all 0 (that is, unmarked, the following default 0 is unmarked, 1 is marked), assuming that the effective memory space is exhausted , JVM will stop the running of the application and start the GC thread, and then start the labeling work. According to the root search algorithm, after the labeling, the state of the object is as shown below.
As you can see, according to the root search algorithm, all objects reachable from the root object are marked as surviving objects.
The first stage marking has been completed. Next, we must perform the second phase of cleaning, then after cleaning, the remaining objects and the status of the objects are shown in the following figure.
It can be seen that the objects that have not been marked will be recovered and cleared, and the marked objects will remain, and the marked
The count is reset to 0. Needless to say, wake up the stopped program thread and let the program continue to run.

2.2. Advantages and disadvantages

It can be seen that the mark-clearing method has been solved, the circular reference problem of the reference counting method, and objects that are not referenced from the root node will be recycled.
Disadvantages:
  • The efficiency is low. Both the marking and clearing actions need to traverse all objects, and the application needs to be suspended during the GC. This experience is very poor for applications with more interactive requirements.
  • Memory fragmentation cleaned up by the mark removal method is more serious, because the objects being cleaned exist in all corners of the memory, so the cleaned memory is not coherent.

3. Mark compression algorithm

The mark compression algorithm is optimized on the basis of the mark removal method. The mark phase is the same. In the cleanup phase, instead of directly cleaning the marked objects, the live objects are compressed to one end of the memory, and then the garbage beyond the boundary is cleaned up Fragmentation problem.

3.1 Principle

3.2 Advantages and disadvantages

Solve the problem of fragmentation of mark removal method, and at the same time, the step of compression, the step of moving the memory location of the object, its efficiency has a certain impact.


4. Copy algorithm

The core of the replication algorithm is to divide the original memory space into two, only use one of them at a time, copy the objects being used to another memory space during garbage collection, and then empty the memory space, exchange two Memory roles complete garbage collection.
If there are more garbage objects in memory and fewer objects need to be copied, it is more appropriate and efficient to use this algorithm in this case, and vice versa.

4.1. Young with memory space in JVM

  • At the beginning of the GC, the object will only exist in the Eden area and the Survivor area named "From", the Survivor area "To" is empty.
  • Immediately after the GC, all living objects in the Eden area will be copied to "To", while in the "From" area, the objects that are still alive will decide where to go based on their age value. Objects whose age reaches a certain value (the age threshold can be set by -XX: MaxTenuringThreshold) will be moved to the older generation, and objects that do not reach the threshold will be copied to the "To" area.
  • After this GC, the Eden and From areas have been emptied. At this time, "From" and "To" will exchange their roles, that is, the new "To" is the "From" before the last GC, and the new "From" is the "To" before the last GC. In any case, it will ensure that the Survivor area named To is empty.
  • The GC will repeat this process until the "To" area is filled. After the "To" area is filled, all objects will be moved to the old generation.

4.2. Advantages and disadvantages

advantage:
  • In the case of many garbage objects, the efficiency is higher
  • No fragmentation of memory after cleaning
Disadvantages:
  • Not applicable in the case of few garbage objects, such as: old generation memory
  • The memory space is divided into two, only one of them is used, and the memory usage rate is low

5. Generational algorithm

A variety of recycling algorithms have been introduced above. Each algorithm has its own advantages and disadvantages. No one can replace anyone, so it is a wise choice to choose according to the characteristics of garbage collection objects. The generation algorithm is actually like this. Choose according to the characteristics of the recycling object. In the jvm, the young generation is suitable for using the replication algorithm, and the old generation is suitable for using the mark removal or mark compression algorithm.

Intelligent Recommendation

Garbage collection and several common garbage collection algorithms

Foreword:   First think about the three things that Garbage Collection (GC) needs to accomplish    1) Which memory needs to be reclaimed?    2) When will it be collected?    3) How to recycle? In the ...

How to determine the object needs to be recycled in the garbage collection mechanism in Java? What are the common GC recycling algorithms?

Judging whether the object needs to be recycled: 1. Reference counting algorithm: Add one to the object (object head)Reference counter,When there is a place to reference it, the counter value adds one...

GC garbage collection algorithms and garbage collector (important, important, very important)

** sericl serial recovery (the recovery, other programs are suspended) Parallel Parallel recovery (to find a plurality of cleaning work, other program has paused) CMS concurrent recovery tag (4 steps,...

How does GC determine garbage? and garbage collection algorithms

1. What is garbage? The object takes up memory resources, but you can never use the object. The general case is that there is no referenced object or an object that does not perform external reference...

JVM--Garbage Collection--Common Algorithms

table of Contents Common garbage collection algorithms 1. Reference counting 2. Mark removal method 3. Marker compression algorithm 4. Copy Algorithm Generational algorithm Common garbage collection a...

More Recommendation

Common garbage collection algorithms of JVM

Algorithm 1:Reference counting method. This method is the most classic method. Specifically, a reference counter is set for the object. Every time a variable references to it, the reference counter wi...

JVM common garbage collection algorithms

In the Java memory model, the program counter, virtual machine stack, and local method stack are independently owned by each thread and "live and die" with the thread. The memory size of eac...

Several common garbage collection algorithms

Common garbage collection algorithms include: mark-sweep algorithm copy algorithm Mark-Collating Algorithm Generational Collection Algorithm The JVM's garbage collection algorithm uses a generational ...

Common garbage collection algorithms and classic garbage collectors

Common garbage collection algorithms and classic garbage collectors How to identify garbage Reference counter Accessibility analysis Common garbage collection algorithms in JAVA Mark removal algorithm...

JVM optimization 2: What is garbage collection, common algorithms, garbage collectors, memory allocation, and visual GC log analysis tools

JVM optimization-2 Today's content Understand what garbage collection is Master the common algorithms for garbage collection Learn serial, parallel, concurrent, G1 garbage collector Learn to visualize...

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

Top