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

How does jvm GC master

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to master jvm GC". In daily operation, I believe many people have doubts about how to master jvm GC. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to master jvm GC"! Next, please follow the editor to study!

Before learning GC, you should first remember one word: "stop-the-world". Stop-the-world can occur in any GC algorithm. Stop-the-world means that JVM stops the execution of the application because it wants to execute GC. When Stop-the-world occurs, all threads, except those required by GC, wait until the GC task is completed. GC optimization often refers to reducing the time it takes for Stop-the-world to occur.

The process in which Young generation objects disappear from this area is called "minor GC".

The process in which Old generation objects disappear from the old age is called "major GC" (or "full GC").

Persistent generation (permanent generation) is also known as method area (method area). GC may also occur in this area, and GC events that occur in this area are also counted as major GC.

# the composition of the new generation

An Eden space (Eden)

Two Survivor spaces (Survivor) in order to understand GC, we must first understand the younger generation used to store all newly created instance objects. The younger generation is divided into three parts:

A) An Eden space (Garden of Eden, I think it's good and weird not to translate literally)

B) two Survivor spaces (survivor space, still no longer literal translation)

There are a total of three spaces: two of them are Survivor spaces, and the execution steps for events in each space are as follows:

Note: the goal is to increase the chances of being recycled before the object is transferred to the older generation.

As you can see from the above steps, one of the Survivor space must be empty. If both of your Survivor space have data, or neither of them has data, it probably means that there is something wrong with your program.

The vast majority of newly created objects are assigned to Eden space.

After Edenspace executes the first GC, the instance objects that continue to exist are transferred to Survivor space.

After Edenspace executes the GC again, the instance object that continues to exist is transferred to the Survivor space that already has the instance object.

Once the Survivor space is full, objects that continue to exist are transferred to another Survivor space. Then the front Survivor space will be emptied.

After the above steps are repeated a certain number of times, the instance objects that are still alive will be transferred to the older generation.

In HotSpot VM, two techniques are applied to speed up memory allocation:

1.Bump-the-pointer

2.TLABs (Thread-Local Allocation Buffers).

The Bump-the-pointer (pointer) technology tracks the location of the last instance object added to the Eden space. This object is placed at the top of the Eden space. When a new object is created, you only need to check the information of the last object stored in Eden space to know whether the new object can be stored in Eden space. If possible, it will be stored on top of the Edenspace. So when a new object is created, the last stored object is detected, and this mechanism greatly improves the efficiency of memory allocation. However, this method is not suitable for multithreaded environment, when multithreaded operation, some threads can not execute normally because of locks, which leads to a great reduction in efficiency.

TLABs is designed for this situation. TLABs allows each thread to use a small portion of Eden space as its own operation object, so that each thread can use Bump-top-pointer technology to speed up memory allocation, thus avoiding thread problems caused by locks.

Newly created objects that are stored in Eden space and are still alive after various processing will be transferred to the older generation.

# GC processing mechanism in the old days

When the older generation is full of instance objects, the execution of "full GC" will be triggered.

SerialGC (- XX:+UseSerialGC) "mark-sweep-compact" (Mark-erase-compress)

Single thread performs recycling operation, pauses the execution of all application threads during recycling, and the default collector in client mode

Younger generation: the GC algorithm used (bump-top-pointer,TLABs). The selection of design is replication.

The collection of the older generation is divided into three steps: Mark, Sweep and Compact.

The marking phase marks out all the living objects.

Release all dead objects in the clearance phase.

In the merge phase, all living objects are continuously discharged from front to back in the heap, and the heap space is divided into two parts, one is stored objects, the other is unstored objects (that is, compressed part). The selection of the design is merging to reduce memory fragmentation.

ParallelGC (- XX:+UseParallelGC)

Using multiple threads for garbage collection at the same time, multi-core environment can make full use of CPU resources, reduce collection time, increase JVM productivity, the default collector in Server mode. Like the serial collector, the execution of all application threads is suspended during recycling

Younger generation: use multiple threads to collect garbage, each using the same algorithm as the serial collector

Old age: the older generation is still single-threaded, the same as the serial recycler

ParallelOldGC (Parallel Compacting GC) (- XX:+UseParallelOldGC)

The older generation is divided into three steps: marking, statistics (summary) and merging.

In the marking phase, all surviving objects are divided into N groups (which should be the same as the number of reclaimed threads). Each thread is independently responsible for its own group, marking the location of the surviving objects and the survival rate information of the area (Region), marked as parallel.

In the statistical stage, the survival rate of each Region is higher in principle. From front to back, find the starting position worthy of merging (the regions where most objects are alive are not worth merging), and the statistical stage is serial (single thread).

In the merge phase, according to the information of the statistical phase, multithreading copies the surviving objects from one Region to another Region in parallel.

The idea of division is used here to divide the older generation into many fixed-size region. -XX:ParallelGCThreads=3 can also further specify the number of threads participating in parallel recycling, which, like the serial collector, suspends the execution of all application threads during recycling. The collection time of the older generation is shorter than that of the parallel collector, thus reducing the pause interval (Pause time) *

Parallel Old GC appears after JDK5. Compared with parallel GC, the only difference is the old GC algorithm. Parallel Old GC is divided into three steps: mark-summary-compaction (tag-summary-compaction). The summary step differs from sweep in that it distributes surviving objects to different areas pre-processed by GC, and the algorithm is slightly more complex than cleanup.

Concurrent Mark & SweepGC (or "CMS") (- XX:+UseConcMarkSweepGC)

There are four steps, initial tagging (Initial Mark), concurrency tagging (Concurrent Mark), retagging (Remark), and concurrency cleanup (Concurrent Sweep). Note that there is no merge operation, so there will be fragments.

Initialization phase: pause the application thread and find out all the surviving objects. Only the surviving objects near the class loader will be marked, so the pause time (stop-the-world) is relatively short, and the recycler uses a single thread. All objects referenced by surviving objects are confirmed during the concurrent marking phase whether they have been tracked and verified.

Concurrent marking phase: the collector marks the operation and the application runs concurrently, and the collector uses a single thread to mark the surviving object.

Marking again: the concurrent marking phase may add or modify objects during this process because the application is also running. So pause the application thread again, find out all the modified objects, and use multithreading tags.

Concurrency cleanup: the collector cleanup operation runs concurrently with the application, and the recycler uses a single thread to clean up dead objects.

Once this GC type is adopted, the pause time caused by GC can be extremely short. CMS GC is also known as low latency GC. It is often used in applications that are very demanding on response time.

Of course, while this GC type has the advantage of short stop-the-world time, it also has the following disadvantages:

1) it takes up more memory and CPU than other GC types

2) Compression steps are not supported by default

Before using CMS GC, you need to do a comprehensive analysis of the system. In addition, when you need to perform a compression task to avoid too much memory fragmentation, CMS GC will bring more stop-the-world time than any other GC, so you need to analyze and determine how often the compression task is executed and how long it takes.

Garbage First (G1) GC

If you want to understand G1 clearly, please forget the knowledge about the new generation and the old age introduced above. As shown in the figure above, each object is analyzed into a grid when it is created, and the subsequent GC is also done in the grid. Each time a region is full of objects, the newly created object is assigned to another region and the GC begins. In this kind of GC, there is no phenomenon that the objects in other GC move in the three regions of Cenozoic and old generations. The G1 is designed to replace CMS GC, which has exposed a lot of problems and complained about it in its long-term use.

The biggest improvement of the G1 is its performance, which is faster than any of the above GC.

The recovery methods of the younger generation (Minor Collection) of categories 2, 3 and 4 are the same, so the above is only introduced for the old age.

Method area:

Stores the class information to be loaded, static variables, constants of type final, properties, and method information. JVM uses permanent generation (PermanetGeneration) to store the method area. (in the HotSpot virtual machine of JDK, the method area can be regarded as permanent generation, but in other types of virtual machines, there is no concept of permanent generation. For more information, please see Zhou Zhiming's book.) you can specify the minimum and maximum values through-XX:PermSize and-XX:MaxPermSize.

At this point, the study of "how to master jvm GC" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report