Which garbage collection algorithms should a Java developer know, and how do they differ?
There are five collectors that you should know about, and they are all in the standard Java virtual machine.
The serial collector uses one thread for everything, the collection and the work of the application. It has the smallest pause times of all, because there is only one thread to stop, and it is the best one for a small heap and for a program with one thread.
The parallel collector uses several threads for the collection and gives the processor to the application between collections. It has the best throughput of all, which means it finishes the most work in the shortest total time, and it is the right one when you have a lot of work and you can afford a pause now and then.
The G1 collector divides the heap into regions and collects the ones with the most rubbish in them. It balances the pause times well, and it is the default collector since Java 9.
The Z collector aims to keep the pauses very short, usually under a millisecond, no matter how big the heap is. It does its work alongside the application, so the cost is in the processor rather than in the pause.
The Shenandoah collector also aims for very short pauses, and it does its work by moving the live objects out of the way while the application runs.
So how do they differ? Two ways. The throughput collector finishes the most work in the least total time. The low pause collectors keep the application responsive, and they use more processor and more memory to do it. You rarely get both at once.
The next question in this topic looks at how to find out which one you have.