Low space efficiency, not available for halfThe solution is to adjust the ratio of the two blocks. In the HotSpot, the memory is divided into one Eden and two worker vor. The ratio is8:1
Each time an Eden, vor a, and vor B are used for backup. During garbage collection, copy the surviving objects of Eden and vor A to vor B, and then recycle Eden and vor. Next, use Eden, assign vor B, and assign vor.
The premise of this method is that a large number of objects are dead each time they are recycled, and only a small part is saved. In this way, just copy a small part. However, if a large number of objects survive for a long period of time, it is necessary to copy back and forth repeatedly, which is a waste of life. From this point, we can also see that different recycling policies should be used according to the characteristics of the object survival time.
Tag Sorting AlgorithmThe tag sorting algorithm is an improvement on the tag clearing algorithm. Mark the object to be recycled during the first scan. Instead of clearing these objects for the second time, they move the surviving objects to the end of the memory area, and they are all connected together. Then, all the places outside the boundary of this area are recycled.
Generational collection AlgorithmFrom the above discussion, we can see that different recycling policies should be used according to the object survival time. Some objects have a short survival time. In this way, there are fewer surviving objects each time they are recycled. You can use the replication algorithm. Some objects have a long survival time. In this way, there are only a few objects to be recycled each time. You can use the tag clearing and tag sorting algorithms. Java heap is divided into the new generation and the old generation. Objects in the new generation have a short survival time and objects in the old generation have a long survival time.
There are two generational criteria: survival time, object size, and large objects entering the old age.
HotSpot Garbage CollectorThe HotSpot garbage collector is divided into the new generation recycler and the old generation recycler by generation. The number of Garbage Collector threads can be divided into single-thread recycler and multi-thread recycler. Whether to stop all working threads during garbage collection can be divided into concurrent recyclers and non-concurrent recyclers. Therefore, to see a recycler, you need to understand it:
- Used for the new generation or the old generation
- Single thread or multi-thread
- Whether to stop all worker threads during work
- By default, the new generation recycler in Client mode adopts the replication algorithm.
- Single thread
- All worker threads need to be suspended, ParNew recycler
- In Server mode, the new generation recycler is preferred, and the replication algorithm is used.
- Multithreading
- No need to pause all working threads
- Only the CMS recycler can work with Parallel Scavenge recycler.
- The new generation recycler uses the replication algorithm.
- Multithreading
- The goal is to achieve a controllable throughput (throughput = running user code time/(running user code time + garbage collection time ))
Other recyclers aim to reduce the pause time of user programs due to garbage collection, while Parallel Scavenge recycler aims at the available throughput, because it is also called the throughput priority recycler. There is a conflict here. If the pause time decreases, the user program can get a faster response, but this means that garbage collection becomes frequent, the overall garbage collection time gets longer, and the throughput drops. Available-XX:MaxGCPauseMillis
You can also adjust the pause time of garbage collection.-XX:GCTimeRation
Adjust the throughput.
Serial Old recycler
- The old recycler adopts the mark sorting algorithm.
- Single thread
- All worker threads need to be suspended
- Use in Client Mode
- Earlier versions of the Serial recycler Parallel Old collector
- The old recycler adopts the mark sorting algorithm.
- Multithreading
- Parallel Scavenge earlier CMS (Concurrent Mark Sweep) recycler
- The old recycler uses the mark clearing algorithm.
- Multithreading
- In general, you do not need to pause all worker threads.
Running process:
- Initial tag
- Concurrent tag
- Remark
- Concurrent cleanup
Among them, the concurrent mark and re-mark need to suspend all user threads, but the two stages are used less than the other two stages. However, it takes a long time to mark and clear concurrent threads, but they can work with user threads.
The initial flag only marks the objects directly associated with the GC Root, and re-marks the objects marked for changes during the modified concurrent flag due to user program running.
G1 (Garbage-First) recycler