Moment For Technology

The garbage collection

Posted on Dec. 2, 2022, 6:31 p.m. by Alyssa Mendoza
Category: The back-end Tag: The back-end java

1. How to locate garbage

1. Reference counting; 2. Root reachable algorithmCopy the code

2. Common garbage collection algorithms

2. Copy algorithm - No shards, waste of space 3. Mark compression - No fragmentation, low efficiencyCopy the code

3. JVM memory generational model (for generational garbage collection algorithm)

1. The model used by some garbage collectors; New Generation + Old generation + Permanent generation (1.7)/Metadata (1.8) Metaspace (not heap, OS management) 1. Permanent generation metadata - class 2. Permanent generation must specify size limit, metadata can be set or not set, no upper limit (limited to physical memory) 3. String constants 1.7- permanent generation 1.8- heap 4. MethodArea logic concepts - permanent generation metadata 3. Cenozoic = Eden + 2 Suvivor zones 1. After YGC collection, most objects will be collected and enter s0 alive; 2. YGC, Eden + s1 - s0; 3. YGC, Eden + s0 - s1; 4. Age enough - old age 5. S zone won't fit - old age 4. The old age 1. The old age is Full of FGC Full GC 5. Minimize FGC; 2. MinoreGC = YGC 3. MajorGC = FGCCopy the code

4. Common garbage collectors

classification

Young generation GC UserSerialGC: serial garbage collector UserParallelGC: parallel garbage collector UseParNewGC: young generation parallel garbage collector UserSerialOldGC: old generation GC UserSerialOldGC: Serial old age garbage collector (since removed) UseParallelOldGC: old age parallel garbage collector UseConcMarkSweepGC: (CMS) Concurrent mark cleanup general UseG1GC: G1 garbage collectorCopy the code
4.1 Serial Collector

A single threaded collector. STW

For a limited single-CPU environment, the highest single-threaded garbage collection efficiency can be achieved without the overhead of thread interaction, so the Serial garbage collector remains the default new generation garbage collector run by the Java virtual Machine in Client mode.

The corresponding JVM parameters are: -xx :+UseSerialGC: When enabled, the collector combination of: Serial(Young) + Serial Old(Old) indicates that both the new generation and the Old generation use the Serial collection collector. The new generation uses the replication algorithm, and the Old generation uses the mark-sorting algorithmCopy the code
4.2 ParNew Collector

Using multithreading for garbage collection, during garbage collection, stop-the-world pauses all other worker threads until it finishes collecting.

It is the new generation of the parallel multithreaded version of the Serial collector. The most common application scenario is to work with the older CMS GC, and the rest of the behavior is exactly the same as the Seria collector. The ParNew garbage collector also suspends all other worker threads during garbage collection. It is the default garbage collector for the new generation of Many Java virtual machines running in Server mode.

Common JVM parameters: -xx :+UseParNewGC Enables the ParNew collector, which only affects the collection of the new generation, but not the old generation. After the above parameters are enabled, the collector combination of: ParNew(Young)+ Serial Old is used. The new generation uses the replication algorithm, and the Old generation uses the mark-collating algorithmCopy the code
4.3 Parallel Collector

Parallel Scavenge is a garbage collector similar to ParNew, which uses a replication algorithm. It is also a Parallel multithreaded garbage collector. It is also known as a throughput first collector. In a word: parallelization of serial collectors in new and old generations.

It focuses on:

Controllable throughput (Thoughput= user-code time running (user-code time running + garbage collection time), i.e. if the program runs for 100 minutes and garbage collection time is 1 minute, the throughput is 99%). High throughput means efficient use of CPU time for tasks that run in the background without requiring much interaction.Copy the code

The adaptive adjustment strategy is an important difference between the ParallelScavenge collector and the ParNew collector. Adaptive tuning policy: The virtual machine collects performance monitoring information based on the current system running condition and dynamically adjusts these parameters to provide the most appropriate pause time (-xx :MaxGCPauseMillis) or maximum throughput.

Common JVM parameters: -xx :+UseParallelGC or -xx :+UseParallelOldGC (mutually active) Use the Parallel Scanvenge collector. When this parameter is enabled, the new generation uses the copy algorithm and the old generation uses the mark-collation algorithm. -xx :ParallelGCThreads= number N indicates how many GC threads are started. CPU 8 N= 5/8 CPU 8 N= Actual numberCopy the code
4.4 ParallelOld Collector

The Parallel Old collector is an Old version of the Parallel Scavenge. It uses a multithreaded mark-file algorithm. The Parallel Old collector is available only in JDK1.6.

Before JDK1.6, the ParallelScavenge collector can be used to match the Serial Old collector of the Old generation. It can only guarantee the throughput of the new generation first, but cannot guarantee the overall throughput. Avenge (Parallel Scavenge + Serial Old)

Parallel Old is to provide the same throughput priority garbage collector in the Old generation. If the system has high throughput requirements, JDK1.8 can give priority to the matching strategy of the new generation Parallel Scavenge and the Old generation Parallel collector. Be insane and insane Parallel application + Parallel Old)

Common JVM parameters: -xx :+UseParallelOldGC ParallelOld collector. After this parameter is set, Parallel in the new generation + Parallel in the Old age.Copy the code
4.5 CMS Collector

The CMS Collector Concurrent Mark Sweep is a collector that aims to obtain the shortest collection pause time.

Suitable for application in the Internet site or B/S system server, this type of application attaches particular importance to the response speed of the server, hoping that the system pause time is the shortest.

CMS is ideal for server-side applications with large memory and CPU cores, and was the preferred collector for large applications before G1.

Concurrent Mark Sweep, Concurrent collection of low pauses, and is executed with the user thread

JVM parameters: -xx :+UseConcMarkSweepGC After this parameter is enabled, -xx :+UseParNewGC is automatically enabled. Using the collector combination of ParNew (Young) + CMS (Old) + Serial Old, the Serial Old will act as a backup collector for CMS errors.Copy the code

The steps are as follows:

1. CMS Initial Mark - Just marks the objects that GC Roots can directly associate with. This is fast and still requires suspending all worker threads. 2. CMS Concurrent Mark with user threads - Process of GC Roots tracing, working with user threads without suspending worker threads. The main marking process, marking all objects. 3. CMS re-marking - In order to fix the part of the object's marking record that has changed because the user program continues to run during concurrent marking, it is still necessary to suspend all worker threads. Since the user thread is still running when the flag is concurrent, the correction will be made before the official cleanup. 4. CMS Concurrent Sweep - Clears GCRoots unreachable objects to work with the user thread without suspending the worker thread. Objects are cleaned directly based on the result of the markup, and since the longest concurrent markup and concurrent cleanup process can now work with the garbage collection thread and the user thread concurrently, the overall view is that the CMS collector's memory reclamation and the user thread are performed concurrently.Copy the code

The advantages and disadvantages:

Advantages: Low pause for concurrent collection. Disadvantages: Concurrent execution, heavy pressure on CPU resources, and the tag removal algorithm adopted will cause a lot of fragmentation.Copy the code

Due to concurrently, CMS threads at the same time will increase in the collection and application of heap memory footprint, that is to say, the CMS must be completed before the old s heap memory used up garbage collection, or CMS recovery fails, will trigger the guarantee mechanism, serial old s collector will be a in the form of STW GC, resulting in larger pause time.

The marker cleanup algorithm cannot defragment the space, the old space will be exhausted over time, and eventually the heap memory will have to be compressed through the guarantee mechanism.

CMS also provides parameters - XX: CMSFullGCsBeForeCompaction (default O, that is, every time memory consolidation) to specify how many times the CMS after collection, compression on a Full GC.

4.6 SerialOld collector

Serial Old is an older version of the Serial garbage collector, which is also a single-threaded collector using the mark-collation algorithm. This collector is also primarily the default senile garbage collector running on the Client's default Java virtual machine.

In Server mode, there are two main purposes (understand, version 8 and later):

Be used with the Parallel Scavenge in releases prior to JDK1.5. Be insane. Use the CMS collector as a backup garbage collection solution.Copy the code
4.7 G1 Collector

G1 is a server-side garbage collector that is used in multi-processor and large-memory environments to achieve high throughput with as much garbage collection pause time as possible. In addition, it has the following features:

1. Like the CMS collector, it can execute concurrently with the application thread. 2. Clearing free space is faster. 3. More time is needed to predict GC pause times. 4. You don't want to sacrifice a lot of throughput. 5. No need for a larger Java Heap.Copy the code

The G1 collector is designed to replace the CMS collector, and it compares to the CMS in the following waysbetter:

1. G1 is a garbage collector with a memory defragmentation process that does not generate much memory fragmentation. 2. Stop The World(STW) is more controllable. It adds a prediction mechanism to The pause time, allowing users to specify The desired pause time. 3. The CMS garbage collector reduces the running time of suspending applications, but it still has the problem of memory fragmentation. Thus, in order to remove the memory fragmentation problem while preserving the advantages of the CMS garbage collector's low pause times, JAVA7 has released a new garbage collector, the G1 garbage collector. 4. G1 was only available in JDK1.7U4 in 2012. Oracle officially plans to make G1 the default garbage collector in JDK9 instead of CMS. It is a service-oriented application collector, mainly used in multi-CPU and large memory server environment, greatly reduce the pause time of garbage collection, improve server performance, gradually replace the CMS collector before Java8. 5. The main change is that the memory regions of Eden, Survivor, and Tenured are no longer continuous. Instead, they become regions of the same size, with each region ranging from 1 MB to 32 MB. A region may belong to Eden, Survivor, or Tenured memory regions.Copy the code

Features:

1. G1 can make full use of the hardware advantages of multi-CPU and multi-core environment and shorten STW as far as possible. 2. On the whole, THE mark-collation algorithm is adopted in G1, and locally, the replication algorithm is adopted, so that memory fragmentation will not be generated. 3. From a macro perspective, G1 no longer distinguishes between the young generation and the old generation. The memory is divided into several independent sub-regions, which can be approximated as a go board. 4. The entire memory area of the G1 collector is mixed together, but it still has to distinguish the young generation and the old generation in a small scope. The new generation and the old generation are retained, but they are no longer physically isolated, but a collection of some regions and do not need to be continuous. This means that different GC methods will still be used to handle different regions. 5. Although G1 is also a generational collector, the entire memory partition does not physically differentiate between young and old, nor does it require a fully independent survivor(to space) heap to prepare for replication. G1 is only logically generational, or each partition can switch back and forth between generations as G1 runs.Copy the code
4.8 the G1 and CMS
-xx :+ USEG1gc-xx :G1HeapRegionSize=n :G1 region size to be set. Values are powers of 2 and range from 1MB to 32MB. The goal is to have about 2048 regions based on the minimum Java heap size. -xx :MaxGCPauseMillis=n: Maximum GC pause time, this is a soft target, and the JVM will try (but not guarantee) to pause less than this time. - XX: InitiatingHeapOccupancyPercent = n: the number of triggered when the GC heap usage, the default is 45. -xx :ConcGCThreads=n: Number of concurrent GC threads. -xx :G1ReservePercent=n: Set the percentage of reserved memory as free space to reduce the risk of target space overflow. The default value is 10%.Copy the code

Developers simply need to declare the following parameters:

Three steps: Start G1+ set maximum memory + set maximum pause time

-XX:+UseG1GC
-Xmx32g
-XX:MaxGCPauseMillis=100
Copy the code

The article reference links: blog.csdn.net/u011863024/...

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.