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 to master JAVA performance tuning and jvm garbage collection

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

Share

Shulou(Shulou.com)06/03 Report--

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

Preface

JVM is a virtualized operating system, similar to Linux and Window, but it is constructed on the operating system to receive class files and translate class into system-identified machine code for execution, that is, JVM shields us from differences in underlying hardware and operation instructions of different operating systems.

Therefore, the most important role of JVM has surfaced, that is, cross-platform. Because JVM shields the underlying details of the operating system for java programs, Java only needs to care about how to compile and load into JVM.

Because JVM receives Class files, not specific languages, it can run on JVM as long as a language can be compiled into a Class file, such as Groovy, Kotlin, Scala, and so on. So another important feature of JVM is language independence, that is, cross-language.

1. JVM memory model and garbage collection algorithm 1. According to the Java virtual machine specification, JVM divides memory into:

New (younger generation)

Tenured (older generation)

Permanent generation (Perm)

New and Tenured belong to heap memory, and heap memory is allocated from the memory specified by the JVM startup parameter (- Xmx:3G). Perm does not belong to heap memory and is allocated directly by virtual machines, but its size can be adjusted by parameters such as-XX:PermSize-XX:MaxPermSize.

Younger generation (New): the younger generation is used to store the Java objects just allocated by JVM.

Tenured: objects that are not recycled after garbage collection in the younger generation will be Copy to the older generation.

Permanent generation (Perm): permanent generation stores Class and method meta-information, which is related to the size, class and method of the project. Generally, 128m is sufficient, and the principle is to reserve 30% of the space.

New is divided into several parts:

Eden:Eden is used to store the objects just assigned by JVM.

Survivor1

Survivro2: the space of the two Survivor is the same. When the object in the Eden is not reclaimed after garbage collection, it will Copy back and forth between the two Survivor. When a certain condition, such as the number of Copy, is met, it will be Copy to Tenured. Obviously, Survivor only increases the length of stay of the object in the younger generation and increases the likelihood of being garbage collected.

two。 Garbage collection algorithm

Garbage collection algorithms can be divided into three categories, all based on the tag-clear (copy) algorithm:

Serial algorithm (single thread)

Parallel algorithm

Concurrent algorithm

JVM will choose the appropriate recovery algorithm for each memory generation according to the hardware configuration of the machine. For example, if the machine has more than one core, it will choose a parallel algorithm for the younger generation. For more information on selection, please refer to the JVM tuning document.

What is slightly explained is that the parallel algorithm uses multithreading for garbage collection, and the execution of the program is suspended during the collection, while the concurrent algorithm is also multithreaded, but the application execution is not stopped during the period. Therefore, the concurrency algorithm is suitable for some programs with high interactivity. After observation, the concurrent algorithm will reduce the size of the younger generation, in fact, it uses a large older generation, which in turn has a relatively low throughput compared with the parallel algorithm.

When will the garbage collection action be performed?

Another question is, when will the garbage collection be performed?

When the memory of the younger generation is full in that year, a normal GC will be triggered, and the GC will only collect the younger generation. It needs to be emphasized that the younger generation refers to the full Eden, and the full Survivor will not trigger GC.

When the old generation is full, it will trigger Full GC,Full GC to recycle the younger generation and the older generation at the same time.

Full GC will also be raised when the permanent generation is full, which will cause the unloading of Class and method meta-information.

Another question is when an OutOfMemoryException will be thrown, not when memory is depleted.

JVM98% 's time is spent on memory recovery.

The memory reclaimed at a time is less than 2%.

Meeting these two conditions will trigger OutOfMemoryException, which will leave a small gap for the system to do some pre-Down operations, such as manually printing Heap Dump.

Second, memory leakage and its solution. Some phenomena before the system crash:

The time of each garbage collection is longer and longer, from the previous 10ms to about 50ms, and the FullGC time is also extended from 0.5s to 4,5s.

The frequency of FullGC is increasing, and the most frequent FullGC is carried out at intervals of less than 1 minute.

The memory of the older generation is getting larger and larger, and no memory is released every time the FullGC is followed by the older generation.

After that, the system will not be able to respond to new requests and gradually reach the critical value of OutOfMemoryError.

two。 Generate dump files for the heap

The current Heap information is generated through JMX's MBean, which is a 3G (the size of the entire heap) hprof file. If you do not start JMX, you can generate this file through Java's jmap command.

3. Analyze dump files

The next thing to consider is how to open the 3G heap information file. Obviously, ordinary Window systems do not have such a large amount of memory and must rely on high-configuration Linux. Of course, we can import graphics from Linux into Window with the help of X-Window. We consider opening the file with the following tools:

Visual VM

IBM HeapAnalyzer

Hprof tools that come with JDK

To ensure loading speed when using these tools, it is recommended that you set the maximum memory to 6G. After use, it is found that these tools can not directly observe memory leaks, Visual VM can observe the object size, but can not see the call stack; although HeapAnalyzer can see the call stack, it can not correctly open a 3G file. Therefore, we choose Eclipse's special static memory analysis tool: Mat.

4. Analyze memory leaks

Through Mat, we can clearly see which objects are suspected of memory leaks, which objects take up the largest amount of space, and the object invocation relationship. In view of this case, there are many instances of JbpmContext in ThreadLocal. After investigation, the Context of JBPM is not closed.

In addition, through Mat or JMX, we can analyze the thread state and observe which object the thread is blocked on, so as to determine the bottleneck of the system.

5. Regression problem

Q: why does garbage collection take longer and longer before the crash?

A: according to the memory model and garbage collection algorithm, garbage collection is divided into two parts: memory marking, clearing (copying), marking part as long as the memory size is fixed for a fixed time, and the copy part is changed. Because there is some unrecyclable memory in each garbage collection, the amount of replication is increased, resulting in a longer time. Therefore, the time of garbage collection can also be used as a basis for judging memory leaks.

Q: why are there more and more Full GC?

A: as a result, the accumulation of memory gradually depletes the memory of the older generation, resulting in no more space for new object allocation, resulting in frequent garbage collection

Q: why is the older generation taking up more and more memory?

A: because the memory of the younger generation cannot be reclaimed, it is increasingly being Copy to the older generation.

Third, performance tuning

In addition to the above memory leakage, we also found that CPU is less than 3% for a long time, and the system throughput is not enough, which is a serious waste of resources for 8core × 16G and 64bit Linux servers.

While CPU is under-loaded, occasionally users take too long to respond to requests, and we realize that the program and JVM must be tuned. It is carried out from the following aspects:

Thread pool: solve the problem of long user response time

Connection pool

JVM startup parameters: adjust the memory ratio and garbage collection algorithm of each generation to improve throughput

Program algorithm: improve program logic algorithm to improve performance

1.Java Thread Pool (java.util.concurrent.ThreadPoolExecutor)

The thread pool used by most applications on JVM6 is the thread pool that comes with JDK. The reason why the mature Java thread pool is wordy is that the behavior of the thread pool is a little different from what we expected. The Java thread pool has several important configuration parameters:

CorePoolSize: number of core threads (latest threads)

MaximumPoolSize: the maximum number of threads. Tasks exceeding this number will be rejected. Users can customize the processing method through the RejectedExecutionHandler API.

KeepAliveTime: the amount of time a thread stays active

WorkQueue: work queue, where tasks executed are stored

The Java thread pool needs to pass in a Queue parameter (workQueue) to hold the tasks executed, while the thread pool has a completely different behavior for different choices of Queue:

SynchronousQueue: a waiting queue with no capacity. The insert operation of one thread must wait for the remove operation of another thread. Using this queue thread pool will assign a new thread to each task.

LinkedBlockingQueue: unbounded queue, with this Queue, the thread pool ignores the maximumPoolSize parameter, only corePoolSize threads handle all tasks, and unhandled tasks are queued in LinkedBlockingQueue

ArrayBlockingQueue: bounded queues, under the influence of bounded queues and maximumPoolSize, programs will be difficult to tune: larger Queue and smaller maximumPoolSize will lead to low load of CPU; small Queue and large pool, Queue will not start as it should.

In fact, our requirement is very simple. We hope that the thread pool, like the connection pool, can set the minimum number of threads, the maximum number of threads, and the minimum number.

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: 246

*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

Development

Wechat

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

12
Report