Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

Code shrimp is a sand carving and funny boy who likes listening to music, playing games and writing as well as most of his friends. The days are still very long, let’s refuel our efforts together 🌈


🔥 Foreword — > Featured column

Self-introduction, to everyone recommend their column 😁, welcome small partners to collect attention 😊

Force link algorithm problem solving area

The small white to learn Java

MybatisPlus column

App crawler Column

PC side crawler column

Big factory interview question column


🌈 Overall structure of the article


🔥JVM garbage collection algorithm

Mark-clear algorithm

The algorithm is divided into “mark” and “clean” stages: first, mark all the objects to be recycled, after the completion of the mark all the marked objects are recycled. Alternatively, you can mark all objects that do not need to be reclaimed first, and then reclaim those that are not.

The advantages and disadvantages:

The execution efficiency is unstable, which is applicable to areas where objects are stored too much, and memory fragmentation is generated when objects are old

What is clearance?

The clear mark does not really empty, but instead stores the address of the object to be cleared in the free address list. The next time a new object needs to be loaded, determine whether there is enough space for garbage, and if so, store it.


Replication algorithm

Divide the available memory into two equally sized pieces and use one piece at a time. When this area of memory is used up, the surviving objects are copied to another area, and then the used space is cleaned up again. In this way, half of the memory range is reclaimed each time.

Advantages:

No memory fragmentation problems for areas with few live objects (new generation), simple and efficient

disadvantages

If there are too many living objects, the overhead of copying objects increases


Mark-collation algorithm

The marking process is still the same as the mark-clean algorithm, but instead of reclaiming the recyclable objects directly, the next step is to move all surviving objects toward one end and then directly clean up memory beyond the end boundary.

Advantages:

Eliminating the fragmentation of memory in the mark-clean algorithm, the JVM only needs to hold a starting address of memory when allocating memory to a new object. Eliminates the high cost of halving memory in the replication algorithm.

Disadvantages:

In terms of efficiency, the mark-collation algorithm is less efficient than the copy algorithm, which has to adjust the address of the reference if the object is referenced by other objects while moving the object. You need to pause the user application throughout the movement.


✨ Generational collection algorithm

The current virtual machine garbage collection adopts generational collection algorithm, which has no new idea, but divides the memory into several blocks according to the different object life cycle. The Java heap is generally divided into the new generation and the old generation, so that we can choose the appropriate garbage collection algorithm based on the characteristics of each generation.

In the new generation, a large number of objects die in each collection, so a “copy” algorithm can be used to complete each garbage collection for a small amount of the cost of copying objects. Older objects have a higher chance of survival, and there is no extra space for allocation guarantees, so we must choose the “mark-clean” or “mark-clean” algorithm for garbage collection.

🔥 Collection algorithms used by common garbage collectors

  1. Serial collector (copy algorithm): the new generation of single-threaded collector, marking and cleaning are single-threaded, the advantage is simple and efficient;

  2. ParNew collector (copy algorithm): A new generation of delegate parallel collector, which is actually a multi-threaded version of Serial collector and performs better than Serial on multi-core CPUS;

  3. Insane. A new generation of Parallel collector that seeks high throughput and efficient CPU utilization. Throughput = user thread time /(user thread time +GC thread time), high throughput can efficiently use THE CPU time, as soon as possible to complete the calculation tasks of the program, suitable for background applications such as the corresponding interaction requirements are not high scenes;

  4. Serial Old collector (mark-collation algorithm): Old single-threaded collector, older version of Serial collector;

  5. The Parallel Old Collector (mark-collation algorithm) : the Parallel Collector, throughput first, older version of the Parallel Avenge collector;

  6. CMS(Concurrent Mark Sweep) collector (mark-sweep algorithm) : The old parallel collector aims at obtaining the shortest collector pause time. It has the characteristics of high concurrency and low pause, and pursues the shortest COLLECTOR pause time.

  7. G1 (overall mark-tidy, locally copy algorithm)


❤ finally

I am aCode pipi shrimp, a prawns lover who loves to share knowledge, will update useful blog posts in the future, looking forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can key three even oh! Thank you for your support. See you next time