Today’s sharing started, please give us more advice ~
Phase 1: Serial garbage collector: prior to jdk1.3.1, Java virtual machines only supported Serial collectors.
Phase 2: Parallel garbage collector: With the emergence of multi-core, Java introduced the parallel garbage collector to make full use of multi-core performance to improve garbage collection efficiency.
CMS: The garbage collector can run at the same time as the application, reducing the duration of suspended user thread execution.
Phase 4: G1 (Concurrent) collector: The original intention is to clean up very large heap space to meet a specific pause application time, with less memory fragmentation than CMS.
1 garbage collection algorithm
1-1 mark clearing algorithm
Description of algorithm
Advantages: Fast recovery speed
Disadvantages: Memory fragmentation, unable to allocate large contiguous space.
Algorithm thought
Before Java9, the default garbage collector for Java was ParallelGC, and G1 has been the default garbage collector since Java9
Step1: The first scan, through the GC root object to determine which objects in the heap memory can be garbage collected, marked.
Step2: Second scan, garbage collect those marked GC root objects, just put the start and end memory addresses into the free memory area.
1-2 mark sorting algorithm
The first step is still the mark, and the second step will do a spatial defragment so that there is no fragmentation.
Advantages: Avoids memory fragmentation
Disadvantages: the arrangement of the space makes efficiency is relatively low.
1-3 Replication algorithm
Features:
The managed memory is divided into two regions, the FROM region and the TO region, and objects that do not need to be reclaimed are copied from the FROM region to the TO region. The memory area is decluttered during replication. And then switch from and to.
Advantages: No memory fragmentation is generated
Disadvantages: Need double memory space, memory utilization is not high, and copy also takes time.
1-4 Summary of three garbage collection algorithms
Note: The actual JVM garbage collection algorithm uses a combination of the above three algorithms.
2. Generation reclamation algorithm of JVM
2-1 overview
In the Garden of Eden garbage
The Cenozoic era is mainly composed of three parts, namely, Eden Area, surviving area from and surviving area to. In general, only the Eden area and the surviving area from will store the number, and the surviving area to will only be used by the replication object in garbage collection. The new generation of heap memory makes a Minor GC, and most objects are collected.
The old generation usually stores objects that are often used, and if an object survives multiple garbage collections, it will be put into the old generation from the new generation. Full GC is only triggered when the new generation is out of memory and the old generation is out of memory.
Why do we need to divide?
In the real world, the life cycle of objects is different. Older objects have a longer life cycle and may only be garbage collected once in a long time. The life cycle of new generation objects is relatively short and garbage collection is more frequent. This partitioning method facilitates more efficient garbage collection by using different garbage collection algorithms.
2-2 Generational garbage collection example
Step1: The program just starts to run, and the generated objects are put into Eden area first. When Eden area cannot be put into Eden area, the generated objects are put into Eden area first.
Step2: perform Minor GC for Eden region, copy the surviving region To of the object that is not garbage collected, and then exchange the surviving region To and surviving region From. The final effect of the first garbage collection is shown as the figure below:
step3: In the first Minor garbage collection, the Eden area is allocated for new objects, and after a while, Eden is out of space. The second Minor garbage collection is triggered, and this time the garbage checks the Eden area and the survivable area From which objects can survive. The objects are copied To the survivable zone To, and then the survivable zone To and survivable zone From are swapped. At this point, the Eden zone is empty again and new objects can be placed.
During the actual garbage collection process, the JVM keeps track of the number of times each object has survived the garbage collection. For example, in the figure above, the number of times two objects in the surviving section have survived the garbage collection is 1 and 2 respectively.
Step4: When the number of times some objects survive through garbage collection reaches a threshold (indicating that the object is of high value), then the object will be moved to the old age.
Consider extreme cases: Eden area, from area and elderly area are all full?
Full GC is triggered (Minor GC first,Minor GC is still out of memory)
2-3 Summary of generational garbage collection
Objects are first assigned to the Eden region
When the minicar runs out of space, the Minor GC is triggered, Eden and surviving from objects are copied to to using copy, the surviving objects are aged by 1 and the from to is swapped.
The minor GC causes stop the world, suspends other users’ threads, and waits until the garbage collection is complete before the user threads resume running. The pauses are shorter, and since most objects in the new generation are garbage, there are few objects that are copied, so it is more efficient.
When the object lifetime exceeds the threshold, it is promoted to the old age, with a maximum lifetime of 15 (4 bits, object header storage).
When the old age runs out of space, the minor GC is tried first, and if there is still insufficient space after that, the full GC, STW, takes longer to fire.
Full GC stops the world for a longer time than MInor GC, because there are more live objects in the old generation and space defragmenting time, so the stop time is longer. An out-of-memory exception is raised if there is still insufficient space after Full GC.
2-4 Vm parameters related to garbage collection
Overview of garbage collector
2-5 Garbage recycling case analysis
Situation 1: Nothing
-Blair: It’s a new generation, Tenured generation
Result of Case 1
You can see that even if the user does not create objects, the system objects occupy a portion of the heap memory space.
Are Java memory objects allocated on the heap
Situation 2: The new generation heap is full, triggering GC
Case 2 Result
Case 3: The new generation of memory outgrows the number of objects
The execution result
The new generation can not put, put the new generation object to the old age.
Case 4: Allocate more memory than the new generation directly at the beginning, and put it directly into the old generation if the old generation can fit
The execution result
When the memory is relatively tight, that is, the new generation is not enough to store, sometimes the object will be directly allocated to the old age, or directly in the case of less recycling times (less than 15 times), the new generation object will be directly moved to the old age.
2 Garbage collector
2-1 Overview of garbage collector
The CMS garbage collector was later replaced by the G1 garbage collector.
2-2 Serial garbage collector
Turn on JVM parameters for the serial garbage collector
-XX:+UseSerialGC = Serial + SerialOld
// Serial: works in the new generation, using a duplicate garbage collection algorithm
// SerialOld: works in the old generation, adopts the tag + collation garbage collection algorithm
Summary: To trigger garbage collection, let multiple threads stop at a safe point, then use a single-threaded garbage collector to collect garbage, and let other threads run when garbage collection is complete.
2-3 Throughput first garbage collector
Turn on JVM parameters for throughput first garbage collector
Parameters for turning on/off
The default multithreaded garbage collector, the former is to open the new generation collector, using the copy algorithm, the latter is to open the old collector, using the tag + copy algorithm. If one of the following options is enabled, the other one will also be enabled.
-XX:+UseParallelGC , -XX:+UseParallelOldGC
Open adaptive dynamic adjustment of the size of the new generation, promotion threshold
-XX:+UseAdaptiveSizePolicy
Two metric adjustment parameters (ParallelGC will adjust the heap size according to the specified metric to reach the desired goal below)
Indicator 1) 1/ (1+ Ratio) = Garbage collection time/total running time
Ratio is 99 by default, meaning garbage collection takes less than 1% of the total time. But it’s usually set to 19.
If the target is not met, ParallelGC adjusts the heap memory size to meet the target, usually larger, so that fewer garbage collections are made, thereby increasing throughput
-XX:GCTimeRatio=ratio
Indicator 2) Time limit for each garbage collection (maximum pause in milliseconds)
The default is 200ms obviously reducing the heap memory space will help reduce the time for each garbage collection
-XX:MaxGCPauseMillis=ms
Conclusion: Obviously indicator 1) is in conflict with indicator 2).
-xx :ParallelGCThreads=n // The number of threads in parallel for garbage collection
Summary: Garbage collection is carried out in a multi-threaded manner. The number of garbage collection threads is usually set according to the number of CPU cores. During the garbage collection phase, parallel garbage collection threads can consume the CPU. In the non-garbage collection phase, the user thread makes full use of CPU resources.
2-4 Response-first garbage collector (CMS garbage collector)
Disadvantages: The token cleanup algorithm used generates memory fragmentation that needs to degenerate into a single-threaded garbage collector, resulting in long response times.
JVM parameters that are enabled
Note that this is a concurrent garbage collection using the tag sweep algorithm, unlike the previous garbage collector, which can run other non-garbage collection threads while garbage collection is taking place (there are also time phases that need to be stopped, but not all phases).
Older concurrent garbage collectors will fail, and the older garbage collectors will degenerate to SerialOld.
-xx :+UseConcMarkSweepGC // Use concurrent mark sweep a garbage collector that works in an older age
-xx :+UseParNewGC // Works in the new generation garbage collector
Important initial parameters
-xx :ParallelGCThreads=n // The number of garbage collection threads in parallel, usually equal to the number of CPU cores (garbage collection parallel phase)
-xx :ConcGCThreads=threads // Number of concurrent threads, usually set to 1/4 of the number of concurrent garbage collection threads (garbage collection concurrent phase)
The other parameters
– XX: CMSInitiatingOccupancyFraction = percent / / garbage recycling memory proportion, the reserved space for floating garbage
-XX:+CMSScavengeBeforeRemark
// Before re-marking, collect garbage for the new generation to reduce the number of concurrent garbage objects, + on, – off
Floating garbage refers to the garbage generated by the user thread during concurrent cleanup and needs to wait for the next concurrent cleanup.
Overview of concurrent workflow:
Step1: In the old age, there is no memory.
Step2 :ConcMarkSweepGC does an initial tag action (the initial tag needs STW to block non-garbage collection threads). The initial tag only marks the root object, so it is very fast and the pause time is very short.
Step3: after completing the initial tag, the previously blocked thread can run again. At this time, the garbage collector thread executes the concurrent tag.
Step4: after the concurrent marking ends, the non-garbage collection thread needs to be blocked again for a so-called re-marking,
Step5: after re-marking is complete, the blocked thread can run again. The garbage collection thread also cleans garbage objects concurrently.
Summary: Initial tagging and re-tagging require thread blocking. In concurrent phase, due to the garbage collection thread take up resources, so the system throughput will be affected by certain effect, but the response speed of system because of the concurrent execution are not subject to garbage collection significantly affect (compared to other garbage collector, STW only requires the initial mark to mark time, and can not block other threads of garbage with clearance).
Today’s share has ended, please forgive and give advice!