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 use JVM tuning technology

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

Share

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

This article introduces the relevant knowledge of "how to use JVM tuning technology". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

(I) tuning tool 1.1 jmap

Check the number of instances and memory usage information. The last bit represents the process id, which can be viewed with the jps command.

Jmap-histo pid

Num: represents serial number instances: represents the number of instances bytes: represents occupied space size classname: represents the name of the class

View heap usage:

Jmap-heap pid

Through this command, you can see the garbage collector, heap parameters, heap usage and other information.

Heap memory dump

Jmap-dump:format=b,file=D:/demo.hprof pid

Copy the JVM running file through the command, and after generating the dump file, you can use the visual analysis tool that comes with JDK to analyze it, and enter it under the command line.

Jvisualvm

Automatically open a visual window and load the files we generated into:

All the information can be seen on the visual page

You can set the Jvm parameter to automatically export Dump files after memory overflow:

-XX:+PrintGCDetails-XX:+HeapDumpOnOutOfMemoryError-XX:HeapDumpPath=D:\ jvm.dump1.2 Jstack

You can find the deadlock by adding the id of the process through the Jstack command. First, write a piece of code to create a deadlock.

Public class DeadLockTest {public static Object lock1=new Object (); public static Object lock2=new Object (); public static void main (String [] args) {new Thread ()-> {synchronized (lock1) {try {System.out.println (Thread.currentThread (). GetName () + "begin"); Thread.sleep (1000) } catch (Exception e) {e.printStackTrace ();} synchronized (lock2) {System.out.println (Thread.currentThread (). GetName () + "end");}) .start () New Thread (()-> {synchronized (lock2) {try {System.out.println (Thread.currentThread (). GetName () + "begin"); Thread.sleep (1000);} catch (Exception e) {e.printStackTrace () } synchronized (lock1) {System.out.println (Thread.currentThread (). GetName () + "end");}) .start ();}}

This code creates the first thread to lock the lock1 waiting for the lock lock2, and the second thread to lock the lock2 waiting for the lock lock1.

First look up the process number through the jps command, which I have here is 15820, and then use the jstack command:

Jstack 15820

Deadlocks can be easily found with this command.

1.3 Jinfo

View the parameters of the running Java project to view the parameters of jvm:

Jinfo-flags pid

You can view all the parameters of jvm

View Java system parameters

Jinfo-sysprops pid

You can see the JDK version, location and other information.

1.4 jvisualvm

Visual JVM monitoring tools, the commands mentioned above, can see visual data directly in jvisualvm, but this tool needs to be used with caution in a production environment because it takes up some resources.

(3) Jstat

Jstat is actually a tuning tool that comes with JVM, but I took it out alone here because this command is the most commonly used in formal tuning of JVM.

The jstat command can see how much heap memory is being used, how many classes are loaded, and so on.

2.1 garbage collection statistics

Assess memory usage and GC stress

Jstat-gc pid

After I executed this code, there was a string of data.

The explanations of all the parameters are put below, combined with garbage collection, you can understand.

S0C: first Survivor size (kb) S1C: second Survivor size S0U: use size of the first Survivor area S1U: use size of the second Survivor area EC:eden area size EU:eden area size OC: old age size OU: old age use size MC: method area size (meta space) MU: method area use size CCSC: compressed class space size CCSU: compressed class space use number YGCT:YoungGC time (s) FGC:FullGC times FGCT:FullGC time (s) GCT: total GC time (s) 2.2 garbage collection ratio statistics

The usage ratio of each district is given:

Jstat-gcutil pid

Parameter description:

S0: first Survivor Zone current usage ratio S1: second Survivor Zone current usage ratio E:eden Zone usage ratio O: old Age usage ratio M: metadata area usage ratio CCS: compression usage ratio YGC:YoungGC times FGC:FullGC times FGCT:FullGC consumption time GCT: total garbage collection consumption time 2.3heap memory statistics

Statistics on the usage of heap memory

Jstat-gccapacity pid

Introduce the parameters:

NGCMN: minimum capacity of the new generation NGCMX: maximum capacity of the new generation NGC: current capacity of the new generation S0C: size of the first Survivor zone S1C: size of the second Survivor area OGCMN: minimum capacity of the old age OGCMX: maximum capacity of the old age OGC: size of the current age OC: size of the current old age MCMN: minimum metadata capacity MCMX: maximum metadata capacity MC: current metadata space size CCSMN: Minimum compressed class space size CCSMX: maximum compressed class space size CCSC: current compressed class space size YGC:YoungGC times FGC:FullGC times 2.4Cenozoic garbage collection statistics

Statistics on the data of new generation garbage collection

Jstat-gcnew pid

Introduction parameters:

S0C: size of the first Survivor area S1C: size of the second Survivor area S0U: size of the first Survivor area S1U: size of the second Survivor area TT: number of times the object survived in the new generation MTT: maximum number of times the object survived in the new generation DSS: expected survival area size EU:eden area size YGC: young generation garbage collection times YGCT: young generation garbage collection time 2.5 Cenozoic memory statistics

Statistics on the usage of Cenozoic memory

Jstat-gcnewcapacity pid

Introduction parameters:

NGCMN: Cenozoic minimum capacity NGCMX: Cenozoic maximum capacity NGC: current Cenozoic capacity S0CMX: first Survivor Zone maximum capacity S0C: first Survivor Zone size S1CMX: second Survivor Zone maximum capacity S1C: second Survivor Zone size ECMX: Eden Zone maximum capacity EC: current eden Zone size YGC: number of Young Generation garbage Recycling FGC: old Age garbage Recycling Statistics

Statistics on garbage collection in the old days

Jstat-gcold pid

Parameter description:

MC: method area size MU: method area use size CCSC: compressed class space size CCSU: compressed class space use size OC: old age size OU: old age use size YGC: younger generation garbage collection times FGC: old generation garbage collection times FGCT: old years garbage collection consumption time GCT: total garbage collection consumption time 2.7. old memory statistics

Statistics on the use of memory in the old years

Jstat-gcoldcapacity pid

Parameter description:

OGCMN: minimum capacity of the old OGCMX: maximum capacity of the old OGC: current size of the old OC: size of the old YGC: number of garbage collection of the younger generation FGC: number of garbage collection of the old FGCT: garbage collection consumption time of the old GCT: total garbage collection consumption time 2.8CNY spatial statistics

Statistics on the metadata space

Jstat-gcmetacapacity pid

Parameter introduction

MCMN: minimum metadata capacity MCMX: maximum metadata capacity MC: current metadata space size CCSMN: minimum compressed class space size CCSMX: maximum compressed class space size CCSC: current compressed class space size YGC: young generation garbage collection times FGC: old generation garbage collection times FGCT: old years garbage collection time GCT: total garbage collection time (3) Analysis of JVM operation

The above knowledge tuning tools, in addition to understanding the meaning of these tools, we also need to know how to use these tools to analyze the operation of JVM.

3.1 Analysis of the growth rate of the younger generation

We all know that the new object will be generated in the Eden area, so we can use the following command:

Jstat-gc pid 5000 10

Execute every 5 seconds, execute 10 times, and then observe the increasing trend of the eden area in these 50 seconds to know the growth rate of the younger generation.

3.2 Analysis of YGC situation

YGCT and YGC are shown in the jstat-gc command, and the average time spent on YGC can be calculated by YGCT/YGC. We can also observe the frequency of YGC by outputting it at regular intervals.

3.3Survival of objects after YGC

After each YGC, the number of eden zones decreases significantly, while the number of survivor and older years increases, so that we can calculate the number of objects that survive after each YGC and infer the rate at which older objects grow.

3.4.Analyze the FGC situation

The idea of analyzing FGC is the same as analyzing YGC. The frequency of FGC is inferred from the growth rate, and the average time of each FGC is calculated by calculating FGCT/FGC.

The optimization idea is to minimize the number of FGC and avoid the impact of frequent FGC on JVM performance, so try not to let objects enter the old era, that is, less than 50% of Survivor survive after each YGC.

(4) GC log

Sometimes when the system is suddenly slow and unable to find the reason, we can print out all the GC logs and analyze the key indicators in the gc logs.

Print the gc log by adding the JVM parameter:

-XX:+PrintGCDetails-XX:+PrintGCTimeStamps-XX:+PrintGCDateStamps-Xloggc:./gc.log

Each GC is printed in the GC log, so all GC can be analyzed.

In the GC log, I will also write a special article to introduce the parameters of the GC log, which will make you more clear.

We can also use tools such as GCeasy to help us analyze GC logs.

This is the end of "how to use JVM tuning Technology". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

*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