Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java virtual machine HotSpot garbage collector?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Java virtual machine HotSpot garbage collector is what", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "Java virtual machine HotSpot garbage collector is what"!

The HotSpot VM offers a variety of garbage collectors, each with its own characteristics, although we will compare them, not to pick the best one. We choose only the collector most appropriate for the specific application.

Cenozoic garbage collector

Serial Garbage Collector (Single Thread)

Open only one GC thread for garbage collection and stop all user threads during garbage collection.

Generally, client-side applications require less memory, do not create too many objects, and the heap memory is not large, so the garbage collector collection time is short, even if all user threads are stopped during this time, it will not feel significantly stuck. Therefore Serial garbage collector is suitable for client use.

Because Serial collectors use only one GC thread, they avoid the overhead of thread switching, making them simple and efficient.

ParNew Garbage Collector (Multithreaded)

ParNew is a multithreaded version of Serial. Garbage cleaning is performed in parallel by multiple GC threads. But the cleanup process still requires Stop The World.

ParNew pursues "low pause time", the only difference between Serial and ParNew is that it uses multi-threading for garbage collection, and its performance will be improved to a certain extent compared to Serial in a multi-CPU environment; however, thread switching requires additional overhead, so it performs worse than Serial in a single-CPU environment.

Parallel Scavenge Garbage Collector (Multithreaded)

Parallel Scavenge, like ParNew, is a multithreaded, new generation garbage collector. But there are big differences:

Parallel Scavenge: Pursue CPU throughput and be able to complete specified tasks in a short time, so it is suitable for background calculations without interaction.

ParNew: Pursue to reduce user pause time, suitable for interactive applications.

Throughput = time to run user code/ (time to run user code + time to garbage collection)

High throughput can be pursued by reducing the time GC performs the actual work, however, running GC only occasionally means that there will be a lot of work to do whenever GC runs because the number of objects accumulated in the heap during this time is high. A single GC takes more time to complete, resulting in higher pause times. Given low pause times, it is better to run GC frequently for faster completion, which in turn leads to throughput degradation.

Set garbage collection time as a percentage of total CPU time with parameter-XX:GCTimeRadio.

Set the maximum pause time of the garbage disposal process with the parameter-XX:MaxGCPauseMillis.

Turn on adaptive policies with the command-XX:+UseAdaptiveSizePolicy. We just need to set the heap size and MaxGCPauseMillis or GCTimeRadio, and the collector will automatically adjust the size of the new generation, the proportion of Eden and Survivor, and the age of the object entering the old era to the closest extent to the MaxGCPauseMillis or GCTimeRadio we set.

Old age garbage collector

Serial Old Garbage Collector (Single Thread)

Serial Old collectors are older versions of Serial, both single-threaded collectors with only one GC thread enabled, both suitable for client applications. The only difference between them is that Serial Old works in the old age, using the "mark-and-organize" algorithm;Serial works in the new generation, using the "copy" algorithm.

Parallel Old Garbage Collector (Multithreaded)

Parallel Old Collector is an older version of Parallel Scavenge, which pursues CPU throughput.

CMS Garbage Collector

CMS(Concurrent Mark Sweep) collectors are collectors that aim to achieve the shortest collection pause time (low pause), which allows user threads and GC threads to execute concurrently during garbage collection, so users do not feel significantly stuck during garbage collection.

Initial tag: Stop The World, tags all objects directly associated with GC Roots with only one initial tag thread.

Concurrent tag: Use multiple tag threads to execute concurrently with user threads. This process performs reachability analysis and flags all obsolete objects. It's slow.

Retag: Stop The World, which uses multiple tag threads to execute concurrently, marking out the new obsolete objects that just appeared during the concurrent tagging process.

Concurrent cleanup: Use only one GC thread, executing concurrently with the user thread, to clean up the objects just marked. This process is time-consuming.

Concurrent tagging and concurrent cleanup takes the longest time and can work with user threads, so overall, CMS collectors 'memory reclamation processes are performed concurrently with user threads.

Disadvantages of CMS:

low throughput

Unable to process floating garbage, resulting in frequent Full GC

Debris space generation using mark-and-clear algorithm

For the problem of generating fragmentation space, you can open-XX:+UseCMSCompactAtFullCollection, which will perform a memory compression after each Full GC is completed, and organize the scattered objects into one piece. Setting parameter-XX:CMSFullGCsBeforeCompaction tells CMS to perform a memory defragmentation after N Full GC.

G1 Universal Garbage Collector

G1 is a garbage collector for server-side applications. It does not have the concept of new generation and old age, but divides the heap into independent regions. When garbage collection is to be performed, the amount of garbage in each Region is estimated first, and each time the garbage collection starts from the Region with the largest garbage collection value, so that the maximum collection efficiency can be obtained.

Overall, G1 is a collector based on a mark-and-clean algorithm, and locally (between two Regions) it is based on a copy algorithm, meaning that memory space fragmentation is not generated during operation.

A question here

An object and the objects it references may not be in the same Region, so when garbage collection, do you need to scan the entire heap memory to perform a complete reachability analysis?

No! Each Region has a Remembered Set, which is used to record the region where all objects referenced in this region are located. When performing reachability analysis, as long as Remembered Set is added to GC Roots, it can prevent traversing the entire heap memory.

If the operation of maintaining the Remembered Set is not calculated, the working process of the G1 collector is divided into the following steps:

Initial tag: Stop The World, tags all objects directly associated with GC Roots with only one initial tag thread.

Concurrent tag: Use a tag thread to execute concurrently with the user thread. This process performs reachability analysis and is slow.

Final tag: Stop The World, executed concurrently using multiple tag threads.

Filter Recycling: Reclaim obsolete objects, Stop The World, and use multiple filter recycling threads to execute concurrently.

Thank you for reading, the above is "Java virtual machine HotSpot garbage collector is what" the content, after the study of this article, I believe we have a deeper understanding of Java virtual machine HotSpot garbage collector is what this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report