Introduction to some recycling algorithms of Java GC

tags: java

Mark removal algorithm

The mark removal algorithm is well understood. It mainly performs two actions, one is marking, and the other is clearing and recycling the marked object memory. The problem with this algorithm is that memory fragmentation will occur. As shown below:

As can be seen from the above figure, serious memory fragmentation occurs after memory recovery, which leads to insufficient memory when allocating some large objects, but the overall memory is indeed sufficient.

Copy algorithm

The implementation of the replication algorithm is relatively concise and clear. It is domineering to divide the memory into two parts. In normal use, only one fixed part is used. When GC is needed, the surviving objects are copied to the other part, and then Clean up all the used memory. As shown below:

From the above figure, we can see that the memory fragmentation problem of mark clearing is solved, but it is obvious that the copy algorithm has another problem, that is, the memory usage is greatly reduced, and the usable memory is only half of the original.

Tag sorting algorithm

Since the mark removal and copying algorithms have their own advantages and disadvantages, it is natural for us to think about whether the two algorithms can be combined, so the mark sorting algorithm appears. The mark phase is the same as the mark removal algorithm. The part that needs to be recycled is first marked. However, the cleaning phase is not directly clearing, but moving the living objects to one end of the memory, and then clearing the remaining parts. As shown below:

Although the algorithm of marking and sorting can solve some of the problems of the above two algorithms, it still needs to be marked first and then moved. The overall efficiency is still low.

Generational collection algorithm

The generational reclamation algorithm is an algorithm that is currently used more. This is not a new algorithm, but only divides the memory. Different areas of the memory use different algorithms. According to the survival time of the object, the memory is divided into the young generation and the old generation. The young generation includes the Eden area and S0 and S1. In the new generation, the replication algorithm is used. When the object memory is allocated, only the Eden and S0 areas are used. When a GC occurs, the surviving objects are copied to the S1 area, and then the copy is repeated cyclically. When an object is still alive after 15 GCs, the object will enter the old age. In the old generation, because there are fewer objects to be recycled each time, it uses a tag sorting algorithm.

 

 

 

Intelligent Recommendation

On several algorithms of java GC

Recently read << >> java virtual machine-depth understanding of this book, for this part of the garbage collection algorithm to be finishing next notes. java runtime data area follows, in ...

Java GC algorithms

GC algorithm Memory garbage collection focuses on java heap and method area. Objects survival judge Determining whether an object has survived there are two ways: Reference count: Each object has a re...

Introduction to several common GC algorithms

This article is mainly about the commonly used GC algorithm (Reference counting、Mark-sweep、Replication algorithm、Mark-sweep algorithm) Make relevant explanations and give a brief introduction to relev...

GC recycling

One-piece code Two JDK 7 test - -XMS20M -XMX20M -XMN10M -XX: + PrintGcdetails -xx: survivorratio = 8 -xx: + useserialgc 1 test results D:\ProgramFiles\Java\jdk1.7.0_80\bin\java.exe -Xms20M -Xmx20M -Xm...

Java memory recycling and common algorithms

1. What is garbage? (In Java) (1) An object does not have any reference to this object (2) Several objects refer to each other circularly, but no reference points to this cycle, then these several obj...

More Recommendation

JAVA memory model and GC recycling mechanism

Foreword: As a qualified Java programmer, I think it is necessary to understand the JVM. It is more necessary to know the common memory points in the code and the GC recycling strategy. This is the be...

Java Virtual Machine (2) - GC Garbage Recycling

table of Contents Java runtime memory: New generation: Eden: From Servivor: To Servivor: Senior: Permanent: Java8 and metadata: GC Garbage Recycling: Judging whether an object can be recycled: Referen...

Java: JVM Garbage Recycling (GC) mechanism

JVM garbage collection algorithm Mark-sweep principle: Scan from the root set node, marked all of the survival objects, and finally scan the entire memory space and clear the applicable occasion that ...

Java memory recycling mechanism GC (4)

Garbage collection algorithm Contemporary collection theory definition: JVM's garbage collection is adoptedDivided collection algorithm, To divide the heap into **New generation and old age**, so that...

GC -Java garbage recycling basic concept

Welcome to pay attention to my public account reading more excellent articles: Java Programming Guide What is Java GC Java GC is an object that is not survived through the GC collector to ensure that ...

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

Top