Home  >  Article  >  Java  >  What are the jvm garbage collection algorithms?

What are the jvm garbage collection algorithms?

青灯夜游
青灯夜游Original
2021-04-22 14:58:4032863browse

JVM garbage collection algorithm: 1. "Mark-Clear" algorithm; first mark all objects that need to be recycled, and then uniformly recycle all marked objects after the marking is completed. 2. Copy algorithm; divide the memory into two equal-sized blocks, and only use one of them at a time. 3. "Mark-Organization" algorithm; 4. Generational collection algorithm.

What are the jvm garbage collection algorithms?

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

JVM garbage collection algorithm


Two concepts:

New generation: An area where objects with short life cycles are stored.
Old generation: an area where objects with long life cycles are stored.

Same points: They are all on the Java heap

1. Mark-clear algorithm

Execution steps:

  • Marking: Traverse the memory area and mark the objects that need to be recycled.
  • Clear: Traverse the memory again and recycle the marked memory.

Illustration:

What are the jvm garbage collection algorithms?

What are the jvm garbage collection algorithms?

##Disadvantages:

    Efficiency issue; the memory space is traversed twice (the first time is marked, the second time is cleared).
  • Space problem: It is easy to generate a large number of memory fragments. When a larger memory is needed, it cannot find a piece that meets the requirements, so the GC has to be started again.

2. Copy algorithm

Divide the memory into two equal-sized blocks, and only use one of them each time piece. When a block is used up and GC is triggered, the surviving objects in the block are copied to another area, and then the useless memory is cleared at once. The next time the GC is triggered, the surviving parts of that block will be copied to this block, and then that block will be erased, and the cycle repeats.

Illustration:

What are the jvm garbage collection algorithms?

What are the jvm garbage collection algorithms?

##Advantages

Relative to the mark-clean algorithm, it solves the problem of memory fragmentation.
  • More efficient (when cleaning memory, remember the first and last addresses and erase them at once).
Disadvantages:

The memory utilization is not high, only half of the memory can be used at a time.
Improvement

Research shows that most of the objects in the new generation are "live and die", that is, the life cycle is very short and the longer the object lives, the The harder it is to recycle. When GC occurs, there are many objects that need to be recycled and very few survive. Therefore, there are very few objects that need to be moved to another memory, so there is no need to divide the memory space 1:1. Instead, the entire new generation is divided into three areas in a ratio of 8:1:1. The largest area is called the Eden area, and the two smaller areas are called To Survivor and From Survivor respectively.

During the first GC, only the surviving objects of Eden need to be copied to To. Then the entire Eden area is recycled. When GC occurs again, copy the surviving Eden and To to From, and repeat this process. In this way, the memory available in each new generation accounts for 90% of the entire new generation, greatly improving memory utilization. [Related recommendations:

Java Video Tutorial

]But there is no guarantee that the surviving objects each time will always be less than 10% of the entire new generation. At this time, the copied past cannot be saved, so here Another block of memory, called the old generation, will be used to perform allocation guarantees and store objects in the old generation. If it is not enough, OOM will be thrown.

Old generation: stores objects in the new generation that have survived multiple recycling attempts (default 15 times).

3. Mark – Collation Algorithm

Because the previous copy algorithm keeps copying when the survival rate of the object is relatively high. Coming over and copying the past is meaningless and a waste of time. Therefore, a "marking sorting" algorithm was proposed for the old age.

Execution steps:

Marking: Mark those that need to be recycled
  • Organization: Let the surviving objects be moved to the memory Move one end of the file, and then clean up the unused memory directly.
Illustration:

What are the jvm garbage collection algorithms?What are the jvm garbage collection algorithms?

##4. Generational collection algorithm

Most commercial virtual machines currently use this generational collection algorithm. This algorithm has no new content. It just divides the memory into the new generation and the old generation based on the survival time of the object. This way Corresponding algorithms can be adopted for different areas. For example:

  • In the new generation, a large number of objects die every time. The old generation is used as a memory guarantee and adopts a copy algorithm.
  • In the old generation, objects have a long survival time, and either mark sorting or mark cleaning algorithms can be used.

The difference between MinorGC and FullGC

MinorGC: Garbage collection that occurs in the new generation. Because of the characteristics of the new generation, MinorGC is very Frequent, the recycling speed is relatively fast, and the amount recycled each time is also large.
FullGC: Garbage collection that occurs in the old generation, also called MajorGC, is relatively slow, about 10 times slower than MinorGc. A FullGC is usually accompanied by multiple MinorGCs.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of What are the jvm garbage collection algorithms?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn