Four common GC algorithms in JVM

tags: JVM  java  


1. Mark-sweep algorithm (Mark-Sweep)

  • The algorithm is divided into two stages: marking and clearing:

    • First mark all the objects that need to be recycled,
    • Then recycle all the objects that need to be recycled.
  • Disadvantages:

    • Efficiency issues, the two processes of marking and cleaning are not efficient. The efficiency is not high, and all objects need to be scanned. The larger the heap, the slower the GC. (The speed of the GC is proportional to the objects in the heap)
    • There is a space problem. After mark cleaning, a large number of discontinuous memory fragments will be generated. Too much space fragmentation may cause insufficient continuous memory to be found in subsequent use and trigger another garbage collection action in advance.
      There is a problem of memory fragmentation. The more GCs, the more severe the fragmentation.


Runtime Stack runtime virtual machine stack-using root search algorithm

Stack as root

Through the root search algorithm, it is calculated that D, G, F, J, M have no references, so they are recycled


2. Mark-Compact (Mark-Compact) algorithm

The marking process is still the same, but the next step is not to directly clean up, but to move all living objects to one end, and then directly clean up the memory beyond the boundary of this end.

  • Advantages: no memory fragmentation
  • Disadvantages: It takes more time to compact than Mark-Sweep


3. Copying algorithm (Copying)

Divide the available memory into two blocks, and only use one of them each time. When half of the memory is used up, only the surviving objects are copied to another block, and then the original entire memory space is cleared at once.

In this way, each time the memory is recycled for the entire half-area, memory allocation does not need to consider the complicated situation such as memory fragmentation, as long as the top pointer of the heap is moved, the memory is allocated in order, which is simple and efficient.

  • Disadvantages:
    • The cost of this algorithm is to reduce the memory to half of the original, which is expensive.
    • When the survival rate of objects is high, the efficiency of the replication collection algorithm decreases.

Today's commercial virtual machines use this replication algorithm to recycle the new generation (newly-created objects are in the new generation).

  • Only need to scan live objects, more efficient
  • No debris
  • Need to waste extra memory as a copy area
  • The replication algorithm is very suitable for objects with a relatively short life cycle, because
    Every time GC can recover most of the objects, the cost of copying is more
    small
  • According to IBM's special research, 98% of Java objects will only survive 1
    GC cycles, suitable for these objects using replication algorithms. and
    Do not use 1:1 to divide the work area and copy area
  • The default size ratio of Oracle Hotspot virtual machine eden and survivor is 8:1:1, which means that only 10% of the memory is "wasted" each time. (The used eden+survivor space is called To Survivor, and the free 10% is called From Survivor)
    1. Divide the memory into a larger eden space and 2 fewer survivor spaces,
    2. Every time I use eden and one of the survivors
    3. When recycling, copy the surviving objects of eden and survivor to another survivor space at one time
    4. Then clean up eden and used survivor

If you don’t want to waste 50% of the space, you need to allocate additional space to guarantee that all objects in the half-area memory are 100% alive, so you can’t directly choose this algorithm in the old generation.


4. Generational Collecting algorithm

Intelligent Recommendation

Introduction to common GC algorithms

Four GC algorithms Overview Reference counting Copying algorithm (Copying) Mark-Sweep Mark compression algorithm (Mark-Compact) to sum up Overview When JVM is performing GC,The above three memory area...

Comparison of GC algorithms involved in the JVM

I. Introduction Recently, I have a little work in the background. I have been reviewing the JVM stuff and it is still very interesting. Second, the algorithm Graphic + point summary Reference count pr...

JVM GC common algorithm

Reference counting(Rarely used): Use a counter to count the number of times the objects in the heap are referenced, and clear the objects that are not referenced 2.Replication algorithm: Every time th...

JVM, GC and common commands

This is a sharing in November. Taking the opportunity of sharing within the group, I re-organized some basic concepts and learning materials of JVM. In this PPT, the part about G1 is relatively rough....

JVM common GC algorithm

JVM garbage collection overview The garbage collection mechanism is Java's signature ability, which greatly improves development efficiency. Nowadays, garbage collection is almost called the standard ...

More Recommendation

Four GC algorithms and GC generational collection algorithm

Four major algorithms of GC 1. Copy algorithm ​ Young generationMinor GC is used in, this GC algorithm uses the copy algorithm (Copying) JVM divides the young generation into three parts: 1 Eden area ...

JVM GC common instructions and GC common tools

E.g:java -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log Hello The printed log is as follows GCView It is a log query tool that can view the overall GC performance in a graphical interface. ...

Detailed explanation of the four GC algorithms

Article Directory 0. How to judge whether an object is alive in Java? 0.1 Reference counting algorithm 0.2 Root search method 1. Copying algorithm (Copying): suitable for the new generation 1.1 Princi...

Familiar with common GC algorithms, familiar with common garbage collectors, with practical JVM tuning experience (1)

Foreword: As an intern, when this sentence appears on your resume: "Familiar with GC's commonly used algorithms, familiar with common garbage collectors, and practical experience with JVM tuning&...

JVM: Four algorithm analysis of GC

This article will focus on the graphical description of the reference counter, the copy algorithm, the mark cleaning method, the implementation principle of marking compression method, and process Ref...

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

Top