JVM Learning (II) garbage collector

Personal blog project address

I hope to help point a star, give me a little star ✨


Garbage collection algorithm on a presentation of the methodology, the garbage collector garbage collection is a concrete realization of memory recovery.

Different manufacturers, different versions of the virtual machine provided by the garbage collector may be very different, the book is mainly about HotSpot VM garbage collector (in JDK1.7 formal offers commercial G1 collector).

HotSpot VM garbage collector

(Source network, tort deleted)

The figure shows the effect of seven kinds of collectors in different years, if there is connection between two collectors, suggests that they may be used with. Area in which the virtual machine, then it belongs to the new generation or old's collector.


A, Serial collectors

Serial collector is the most basic, the development of the oldest collector, once (before JDK1.3.1) is the only choice for the new generation of virtual machines collected.

  • Briefly: The collector is a collector of a single thread, but it's "single-threaded" meaning not only that it will only use one CPU or a garbage collection thread to complete the collection, the more important it is in when garbage collection, threads must suspend all other work until the end of its collection. So with "Stop The World" title (due to the suspension of all other threads, it is not cool = - =)
  • Scenario: It still is the default virtual machine running the new generation of collectors in the Client mode
  • Superior to other collectors place: simple and efficient (compared to other single-threaded collector), to define a single CPU environments, Serial no collector due to the overhead of thread interactions, concentrate on garbage collection can get the highest natural the single-threaded collection efficiency.

Two, ParNew collector

  • Brief: ParNew collector is actually Serial Collector's multi-threaded version , in addition to the use of multiple threads for garbage collection, the other acts include all control parameters Serial Collector available collection algorithms, Stop The World, object allocation rule, so the recovery strategy and Serial collectors exactly the same.

  • Scenario: Preferred Server in a virtual machine under the mode of the new generation of collectors, one of the reasons, is in addition to Serial collector, it currently only works with the CMS collector. Unfortunately, CMS as a collector of old age , but can not work with JDK1.4.0 that already exist in the new generation of collectors Parallel Scavenge, so when JDK1.5 use CMS to collect old age, the new generation can only choose Serial ParNew or a collector . ParNew collectors also use ** - XX: + UseConcMarkSweepGC after the default options for the new generation of collectors, you can also use the -XX: + UseParNewGC ** option to force it to develop.

  • ParNew and Serial comparison: ParNew collected in a single-CPU environment will never be better than Serial collector effect, even because of the overhead of thread interaction , the collector is achieved by two CPU's Hyper-Threading technology environment We can not guarantee 100% beyond Serial collector. Of course, as the number of CPU that can be used to effectively utilize system resources GC when it is still very good. The same amount it turned on by default and the number of CPU threads to collect, and in a lot of CPU, you can use ** - XX: ParallelGCThreads parameter to limit the number of threads ** garbage collection.


Three, Parallel Scavenge collector: **

  • Brief: Parallel Scavenge collector is a new generation of collectors, it is also using the copy algorithm of the collector, it is a parallel multi-threaded collectors .

  • Scenario: the shorter the dwell time, the more suitable program requires user interaction, high throughput can be efficient use of CPU time computing tasks to complete the program as soon as possible , mainly for operations in the background without the need for too many tasks to interact.

  • Comparative analysis:

    • Features Parallel Scavenge collector is its concern with other collectors different concerns such as CMS collector is to shorten the time for garbage collection Caton user thread as much as possible , and Parallel Scavenge collector target is to reach a can control throughput (throughput) . Throughput is called a ratio of the total CPU time consumed CPU time and run user code, i.e., a certain time = running user code / (m + user code running time garbage collection time) . Since the close relationship with the throughput, Parallel Scavenge collector is also often referred to as "throughput priority" collector.
    • And Parallel Scavenge collector differs ParNew collector: Parallel Scavenge collector has one parameter ** - XX: + UserAdaptiveSizePolicy **, which is a switching parameter, when this parameter is opened, there is no need to manually specify the size of the new generation (-Xmn), the proportion of Eden and Survivor region (-XX: SurvivorRatio), promoted to the object's old size (-XX: PretenureSizeThreadhold) and other details of the parameters, dynamically adjust these parameters to provide the most appropriate time to pause or maximum throughput this adjustment is called adjustment of the adaptive strategy GC (GC Ergonomics)

Four, Serial Old collectors **

  • Brief: Serial Serial collector Old is old's version of this, it is also a single-threaded collectors , use ** "mark - finishing" algorithm **.

  • Scenario: The main significance lies in the collector also to under Client Mode virtual machine to use. If Server mode under which there are two major purposes:

    • In JDK1.5 and earlier in the Parallel Scavenge collector with use .
    • In concurrent collector occur as a backup plan CMS collector Concurrent Mode Failure to use when.

Five, Parallel Old collectors

  • Brief: Parallel Parallel Scavenge collector Old is the old version's use of multi-threading and ** "mark - finishing" law **. The collector is provided in JDK1.6 began, and before that, the new generation of Parallel Scavenge collector has been in a state of embarrassment, because if the new generation chose Parallel Scavenge collector's addition to the old Serial Old (PS MarkSweep) have no choice other than the collector. As the old year Serial Old collect on the service end application performance of "drag", using the Parallel Scavenge collector may not be able to get to maximize the throughput effect on the overall application, due to the centralized collection of older single-threaded server was unable to take full advantage of multi-CPU processing power, in large and more advanced hardware environment of the old era, this combination of throughput is not even necessarily a good combination ParNew plus CMS.

  • Scenario: until Parallel Old collectors appeared, "throughput priority" collector finally have a true comparison application portfolio, focusing on throughput and CPU resources in sensitive applications, can give priority to add Parallel Old Parallel Scavenge collector.


Six, CMS collector **

  • Brief: the CMS (Concurrent Mark Sweep) is a collector for the shortest recovery time objectives pause collector. CMS collector is based on the ** "mark - sweep" ** algorithm, the whole process is divided into four steps:

    • The initial mark (CMS initial mark) just mark what objects can be linked directly to the GC Roots, very fast.

    • Concurrent mark (CMS concurrent mark) process GC Roots Tracing the

    • Relabeled (CMS remark) in order to correct the user program during concurrent mark due to the continued operation which led to marked record that part of the object tag fluctuates, the dwell time at this stage usually slightly longer than the initial mark phase, but, far more concurrent mark short time.

  • Concurrent Clear (CMS concurrent sweep)

Among them, the initial mark, re-mark these two steps still need ** "Stop at The World" . Since the whole process takes the longest concurrent mark and concurrent removal process ** collector threads can work together with the user thread, so, in general, the memory recovery process CMS collector is executed concurrently with the user thread .

  • Advantages: concurrent collection, low pause

  • Disadvantages:

  • CMS collector is very sensitive to CPU resources in concurrent stages, although it does not lead to the user thread Caton, but because of the occupation of a part of the threads (or CPU resources) caused by slow applications, the total throughput will be reduced. CMS enabled by default recovery thread is (the number of CPU +3) / 4, that is, when the CPU is in 4 above, concurrent garbage collection thread of not less than 25% of the CPU resources, and with the increase and decrease of the number of CPU . But when the CPU is less than four, the impact on the CMS user program may become larger.

  • CMS garbage collector can not handle floating (Floating Garbage) may appear "Concurrent Mode Failure" caused by the failure to produce another of Full GC. As the clean-up phase of CMS concurrent user thread is still running, with the program running naturally there will continue to generate new garbage, garbage appear after this part of the marking process, CMS can not dispose of them at times when the collection had to wait until the next GC when then cleared away. This part of the garbage has become a "floating garbage." As the user threads need to run garbage collection phase, you will also need to set aside enough memory space for user threads, so the CMS collector can not wait until as old as the other collector's almost done and then fill up the collection, need to set aside part of the space program provides operational use of concurrent collection.

  • At the end of the collection will generate a large amount of space debris "mark - to clean up" algorithm, there will be plenty of space debris at the end of the collection means. When too much space debris, will bring great trouble to the large object allocation, there will always be a lot of room remaining years old, but can not find a large enough contiguous space to allocate the current object and had to trigger a Full GC in advance.


Seven, G1 collector **

  • Brief: ** G1 (Garbage-First) ** collector is one of the most cutting-edge technology development achievements of the current collector. It is a garbage collector for the server application, have the following characteristics:

  • Parallel and concurrent G1 can take full advantage of multi-CPU, multi-core hardware environments at times, the use of multiple CPU to shorten the Stop-The-World's pause time, some other collectors would otherwise require GC pause Java thread execution action, G1 collector still It allows Java programs by way of concurrent continue to run.

  • Generational collection Although the G1 may not be needed with other collectors will be able to independently manage the entire GC heap, but it can use different ways to deal with the newly created object and has survived for some time to get through GC old object multiple times to get better collection results.

  • Spatial integration with the CMS "mark - clean-up" of different algorithms, G1 on the whole is based on the "mark - finishing" collector algorithm from the local (between two Region) point of view is based on the "copy" of the algorithm , but in any case, these two algorithms mean that no operation during the G1 memory space debris collected can provide regular memory available. This feature is conducive to long-running programs, not because they can not find a contiguous memory space prematurely trigger the next GC when allocating large objects.

  • Predictable pause This is the G1 relative to the other big advantage of CMS, G1 is reduced dwell time and the CMS common concern, but in addition to the pursuit of low-G1 pause outside, but also dwell time model to establish a predictable, allowing use It is explicitly specified length of time in a segment of M msec, the time consumed in the garbage collection can not exceed N milliseconds, which is characterized in almost real-time Java (RTSJ) of the garbage collector.

The scope of the collection of other collectors before the G1 is a whole new generation or old's, and G1 is no longer the case. When using the G1 collector, Java heap memory layout will vary greatly with other collectors, it will be the entire Java heap is divided into separate areas (Region) a plurality of equal size, although there are also retained the concept of the new generation and the old age but the new generation and the old year is no longer physically separated, and they are part of the Region (not necessarily consecutive) collection.

G1 collector model has been able to establish a predictable pause time, because it can be planned to avoid heap garbage collection across the region throughout Java. G1 track individual Region inside the garbage accumulation value size (space recovery obtained experience and the time required to recover), in the background to maintain a priority list, according to each collection time allowed, the maximum recovery value of priority Region ( this is also the reason behind the name is Garbage-First). This use of memory space and Region division have priority area recovery methods to ensure the G1 collectors can obtain the highest possible collection efficiency in a limited time.

  • G1 collector operation can be divided into the following steps:
    • The initial mark (Initial Marking) initial mark phase just mark what the object GC Roots can be directly linked to the time, and modify the value of TAMS (Next Top at Mark Start), so that the next phase of user programs run concurrently, able to correct available Region to create a new object, at this stage need to pause the thread, but it takes very short.

    • Concurrent mark (Concurrent Marking) concurrent mark phase is the beginning of the heap objects reachability analysis from GC Root, find live objects, this stage takes a long time, but may be complicated by the user program execution.

    • The final mark (Final Marking) The final stage is marked for amendments made during the concurrent mark due to the continued operation of the user program which led to marked changes in that part of the generated record mark, the virtual machine will be recorded in this period of time the object changes Remembered Set Logs inside, the stage Remembered Set Logs data will be incorporated into Remembered Set, which need to pause the thread stage, but can be executed in parallel.

    • Filter Recycling (Live Data Counting and Evacuation) screening recovery stage first on the recovery value and cost of each Region to sort, to develop recycling programs can actually be done at this stage concurrently with the user based on the user program expected GC pause time, but because only recover a portion of Region, time is user-controllable, but stop user threads will greatly enhance the collection efficiency.


Ending

Above the record so much garbage collector characteristics and advantages and disadvantages, and did not say which the best, that is to see the specific business requirements using it.

Once we understand the GC algorithm and the garbage collector, the next learn about JVM commonly used commands right.

Guess you like

Origin juejin.im/post/5ce8f1b36fb9a07ed657aed4