JVM's garbage collector [four]

If garbage collection algorithm methodology memory is recovered, then the garbage collector is the concrete implementation. Java virtual machine specification does not specify how the garbage collector should achieve. This article discusses the HotSpot virtual machine after JDK1.7 (G1 provides a collector after 14 Update).

HotSpot virtual machine contains a garbage collector: Here Insert Picture Description
where the cable can be connected with the use of the garbage collector area indicates where part of the new generation of collectors or collector's old.

Serial Collector

Serial collector is the most basic and oldest collector, once (before JDK1.3.1) is the only choice for the new generation of virtual machine collector. At the name is a single-threaded collector, but here the "single-threaded" is not only that it will only use a garbage collection thread to complete the collection, it is importantWhen it is carrying out garbage collection, you must suspend all other thread work until the end of the collection.

Serial / Serial Old collector runs diagram: Here Insert Picture Description
Even now, it is still the default virtual machine running the new generation of collectors in Client mode. Its advantages: simple and efficient (if it is a single-CPU environment, because there is no overhead of thread interaction). Suitable for desktop applications.

ParNew collector

ParNew is multi-threaded version of the Serial, except for the use of multi-threaded garbage collection, the rest including the control parameters (-XX: SurvivorRatio, -XX: PretenureSizeThreshold , -XX: HandlePromotionFailure ...) Serial collector used to collect algorithm, recycling strategies We are the same, the realization reuse a lot Serial code.
It is the first choice for the new generation of collectors virtual machine under Server mode, while only it and Serial can be used with CMS collector. If the JVM parameters set -XX: UseConcMarkSweepGC, then it is the default new generation of collectors, in addition, you can display a specified (-XX: UseParNewGC).
It defaultsOpen the same number of threads and CPUWhen the CPU more time, you can use -XX: ParallelGCThreads specified.
Here Insert Picture Description

Parallel Scavenge collector

Compared with CMS Watch shorten the pause time garbage collector thread of the user as much as possible, the characteristics of Parallel Scavenge collector isIt focuses on the throughput of control. Pause time without explanation, high throughput can be efficient use of CPU, computing tasks to complete as soon as possible for the task background operation. Another important difference Paraller Scavenge collector and ParNew collector is that it has adaptive strategies.

It provides precise control of the throughput of the two parameters, namely the control -XX maximum pause time garbage collection: MaxGCPauseMillis throughput and size disposed directly -XX: GCTimeRatio. -XX: MaxGCPauseMillis allows you to set the number of milliseconds greater than 0, the collector will ensure the best possible recovery of memory it takes no more than this value, do not know if the system does not recommend setting because GC pauses at the expense of throughput and the cost of the new generation of space setting bad could backfire; -XX: GCTimeRatio is an integer from 0 to 100, represents the ratio of the total garbage collection time period, corresponding to the inverse of throughput, the default 99.

Further, there is a switching parameter -XX: UseAdaptiveSizePolicy, no need to manually open the specified size of the new generation (-Xmn), Eden, and Survivor ratio (-XX: SurvivorRatio), promotion of the object's age old (-XX: PretenureSizeThreshold) and other parameters, the virtual systems dynamically adjust these parameters according to operating conditions to provide the most appropriate time to pause or maximum throughput, this adjustment is called "GC adaptive strategy. "Generally you can set the basic parameters (-Xmx maximum heap) and MaxGCPauseMillis (more concerned with maximum dwell time) or GCTimeRatio (more concerned with maximum throughput) to set up a goal to optimize virtual machine, the virtual machine automatically.

Serial Old collectors

Serial Old old's version of the Serial collector, use the "mark - finishing" algorithm. Client mode is that the major to the virtual machine; in Server mode, has two purposes: 1, and Parallel Scavenge JDK1.5 before with the use of 2, an alternative CMS collector.

Parallel Old collectors

Parallel Old Parallel Scavenge collector is a collector's version of the old, use the "mark - finishing" algorithm, JDK1.6 only. Prior to this, if the new generation of collectors and Parallel Scavenge only be used with Serial Old, Server mode, CPU performance and more time is very low. So in fact the basic useless.
In focusing on certain sensitive and CPU resources, you can use a combination of Parallel Old and Parallel Scavenge.
Here Insert Picture Description

CMS collector

CMS (Concurrent Mark Sweep) collector is to obtain a minimum recycling target pause time, you can see from the name, the "mark - sweep" algorithm, its operation process consists of four steps:

  1. The initial mark: mark what objects can be linked to GC Roots, very fast.
  2. Concurrent mark: GC Roots Tracing the process, it is slow.
  3. Relabeled: Fixed concurrent marking phase as a result of the user thread to continue to generate marked record mark changes, very fast, a bit slower than the initial mark.
  4. Concurrent clear
    where the initial marking and re-marking needs STW. As the longest concurrent mark and concurrent removal process can work with user threads, so the overall look: CMS collection procedures and user threads are executed concurrently together.
    Here Insert Picture Description
    Advantages:
    concurrent collection, low standstill.
    Disadvantages:
    Very sensitive to CPU resourcesAlthough not result in the user thread pause, but will take up the thread, the total throughput will be lower, CMS default startup number of threads is recovered (CPU number + 3) / 4;
    We can not handle floating garbageFloating garbage is cleared when the user threads the garbage generated;
    Plenty of space debris, CMS parameters -XX: UseCMSCompactAtFullCollection switch parameter (enabled by default), Full GC pre-merger consolidation of space debris, memory consolidation process is not concurrent, dwell time will be longer.

Why CMS to use the "mark - sweep" algorithm? Because if replaced - after "finishing mark" algorithm, the garbage, the rest of the objects but also the way finishing, these objects can cause memory address changes,At this time, other threads still working, if the referenced object address has changed, the consequences Yes. . .

G1 collector

G1 (Garbage-First) is a garbage collector for server applications, HotSpot development team gives its mission is to be replaced after JDK1.5 release of CMS collector. In JDK1.9, G1 has become the default mode under Server garbage collector, replacing (Parallel Scavenge and Parallel Old combination). It has the following characteristics:
Concurrent and Parallel:
take full advantage of multi-CPU, multi-core hardware advantages, allowing concurrent java program by way continue.
Generational collection:
retain the generational concept.
Spatial Integration:
On the whole, the "mark - finishing" algorithm, from a local perspective (between two Region), a "copy" algorithm, so no memory fragmentation.
Predictable pause:
you can establish predictable pause time model.

G1 the Java heap into equal size areas more (Region), while retaining the concept of the new generation and the old age, but the new generation and the old year is no longer physically separated, they are part of the Region's collection (that is, a region may belong to a new generation of Eden or Survivor areas, may also belong to the old year). Each parameter can be the size of Region -XX: G1HeapRegionSize setting ranges 1MB ~ 32MB, and N is a power of two.

Region has a special area of ​​Humongous, designed to store large objects. G1 as long as Region capacity is more than half of the objects large objects. If an object size exceeds the capacity of the entire Region, will be stored in N consecutive Humongous area, G1 will conduct most of Humongous area as the old era.

G1 track individual Region inside the garbage accumulation value size (space recovery obtained and the time required for recovery), in the background to maintain a priority list, according to each collection time allowed, the maximum recovery value of priority Region, effective avoid whole heap of garbage collection range. This is actually beneficial to run the program a long time (to solve the problem of CMS will produce memory fragmentation).

Thinking: An object and objects referenced within it may not be the same Region, the garbage collector will scan the whole heap of time to complete the analysis it up?
The answer is not necessary, because each Region has a Remembered Set (Memory Set), this object is used to record the Region Region in all referenced objects are located, reachability analysis, as long as the coupled GC Roots Remembered Set can be prevented through the entire heap memory.

Its operation process is divided into:
1. Initial tag: CMS and similar
2. concurrent mark: CMS and similar
3. Final numerals: CMS relabeled and similar
4. Filter Recycling: recycling of waste objects, the STW is triggered, and have respective Region of recovery value and the cost of sorting.
In addition to concurrent mark, the remaining stages of the plenary trigger STW. G1 is not purely pursue low-latency, the official standard is setImprove throughput in the case of controllable delay

to sum up:

Common garbage collector combination:
Serial + Serial Old -> -XX: + UseSerialGC
ParaNew the CMS + -> -XX: + UseParNewGC -XX: + UseConcMarkSweepGC
Parallel Parallel Scavenge + Old -> -XX: + UseParallelGC -XX: + UseParallelOldGC
G1 -XX: + UseG1GC

Published 11 original articles · won praise 0 · Views 612

Guess you like

Origin blog.csdn.net/fei1234456/article/details/104962384