Garbage collector

The previous algorithms are theoretical knowledge, and the garbage collector is the implementation of these algorithms.

Java -xx :+PrintCommandLineFlags -version, Java -xx :+PrintCommandLineFlags -version The following figure shows the result.

ParallelGC is the JDK’s default garbage collector combination, including the PS Scavenge and PS MarkSweep. You can also print out the garbage collector in use using the code below.

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class GCTest {
	    public static void main(String args[]) {
	        List<GarbageCollectorMXBean> list = ManagementFactory.getGarbageCollectorMXBeans();
	        for(GarbageCollectorMXBean bean : list) { System.out.println(bean.getName()); }}}Copy the code

A list of common parameters related to garbage collector composition is also given here.

parameter describe
UseSerialGC The Serial collector and Serial Old collector are used to reclaim the memory
UseParNewGc Reclaim memory using a combination of ParNew + Serial Old collectors
UseConcMarkSweepGC Reclaim memory using a combination of ParNew + CMS + Serial Old collector
UseParallelGC The default value of the virtual machine running in Server mode, using the PS Parallel + Serial Old (PS MarkSweep) collector combination to reclaim memory

Why not one garbage collector, but a combination of garbage collectors? Previously, we mentioned that the generational collection algorithm divides the heap memory region again, integrating the advantages of other algorithms. Therefore, mainstream virtual machines also use different algorithms to implement garbage collectors according to the memory region of different generations. The following will introduce these garbage collectors one by one.

Before introducing the garbage collector, let’s imagine a cleaning scene. If someone is cleaning at one side and someone is throwing garbage at the other, can the sanitation be cleaned up? The answer is definitely no. So how do you clean it up? In a Java virtual machine, the garbage collector is like a garbage collector. There may be one or more garbage collectors, i.e. single-threaded or multi-threaded. The other threads are like garbage collectors. The garbage collector must also take time to clean up, during which time no other threads can “trash”, that is, suspend the use of other threads until the garbage collection is complete. In Java virtual machines, this kind of thing is called “Stop The Word,” or STW. Computers run so fast that if you shrink the STW down to a very short time, people won’t notice it at all. With that in mind, let’s take a look at each of these garbage collectors.

1. Serial collector

Serial in name, this collector is a single-threaded new generation collector. Serial is implemented with a “copy algorithm”. In a single-CPU environment, the Serial collector has no overhead of thread interaction and can theoretically achieve the highest single-thread execution efficiency. STW times can also be controlled within tens to hundreds of millimeters, which is perfectly acceptable.

Serial Old collector

The Serial Old collector, an older version of the Serial collector, is also a single-threaded collector that uses a “mark-collation algorithm.” Because the PS MarkSweep collector is so close to the implementation of the Serial Old collector, many official sources directly replace the PS MarkSweep with Serial Old, So the Ps MarkSweep collector can be thought of as an alias for the Serial Old collector.

ParNew collector

The ParNew collector is essentially a multithreaded version of the Serial collector. The collection algorithm, STW, rules for object allocation, collection strategy, and so on are all exactly the same as the Serial collector, with a lot of code in common. Although the ParNew collector has the advantage of multithreading, it is not necessarily better than Serial in single-CPU and multi-CPU environments, and it is certainly not as good as Serial in single-CPU environments. It seems that more people have more power, but the effect is not satisfactory due to the time overhead of thread interaction. The advantage of multi-threading is to use CPU more efficiently and improve CPU throughput, that is, CPU idle time is little.

4. Parallel avenge

The Parallel Scavenge collector, like the ParNew collector, is a new generation collector that uses replication algorithms and is a Parallel, multithreaded collector. Compared to the ParNew collector, the Parallel Scavenge collector increases precise control of CPU throughput and STW times, allowing faster completion of tasks with less interaction.

Parallel Old collector

The Parallel Old collector is an older version of the Parallel Avenge collector, using multithreading and a “mark-collation algorithm”. The Parallel Collector collector as a Cenozoic collector, can only be used as a Serial Old collector, which is a waste of multiple CPUS. The Parallel Collector + Parallel Old collector is a combination of the insane and the Insane.

6. CMS collector

CMS (Concurrent Mark Sweep) collector from the English name is based on the “mark-sweep algorithm”, and also has the characteristics of concurrency, it is a collector to shorten the STW time as the goal, for some sites that value service response speed, certainly the shorter STW, The user experience gets better. However, the disadvantage is that a large amount of space debris can be generated after garbage collection, which can also be seen from the algorithm used.

G1 collector

The G1 collector is currently the most advanced collector. It is based on a “mark-de-clutter algorithm”, so it does not generate memory fragmentation and can accurately control the time of STW. The G1 collector is suitable for both the new generation and the old generation, giving priority to areas where the most garbage is collected.

The combination of several garbage collectors is shown below.

Think the article is good, you can pay attention to the programming heart wechat public number, reply to any keywords have surprise! Along the way, we grew up together.