8
头图

Interviewer : you like to talk about JVM tuning today?

Interviewer : Have you ever had the experience of tuning JVM in a production environment?

Candidate : None

Interviewer :...

Candidate : Well...it is the case, our general idea of optimizing the system is this

candidate : 1. Generally speaking, the relational database is the first to the bottleneck, first check whether it is a database problem

candidate : (In this process, you need to evaluate whether the index you built is reasonable, whether you need to introduce a distributed cache, whether you need to sub-database and sub-table, etc.)

candidate : 2. Then, we will consider whether we need to expand (both horizontal and vertical)

candidate : (During this process, we will suspect that the system is under too much pressure or the system's hardware capabilities are insufficient to cause frequent system problems)

candidate : 3. Next, check and optimize at the application code level

candidate : (Expansion cannot be endless, there is money inside and out. In this process, we will examine whether there is a waste of resources in the code we write, or whether there is a logical optimization. For example, processing certain requests in parallel)

candidate : 4. Next, check and optimize at the JVM level

candidate : (After reviewing the code, in this process, we observe whether the JVM has multiple GC problems, etc.)

candidate : 5. Finally, the network and operating system level investigation

candidate : (This process checks whether the memory/CPU/network/hard disk read and write indicators are normal, etc.)

candidate : In most cases, the third step is over. Generally, the JVM and machine parameters set by the "Operation and Maintenance Team" have met most of the needs.

Candidate : Before, other teams discovered the problem of interface processing timeout in the "big promotion". At that time, various monitoring investigations were suspected to be caused by FULL GC.

candidate : The first idea is not to adjust various JVM parameters for optimization, but to directly add the machine

candidate : (using the crudest method, solving the problem is the simplest, expand the YYDS)

Interviewer : Indeed

Candidate : However, I have learned JVM-related tuning commands and ideas.

candidate : In my understanding, tuning the JVM is actually to "understand" the JVM memory structure and various garbage collectors, and combine your existing business to "adjust parameters" to enable your own applications Normal and stable operation.

candidate : general tuning JVM we think there will be several indicators to refer to: "throughput", "pause time" and "garbage collection frequency"

candidate : Based on these indicators, we may need to adjust:

candidate : 1. Memory area size and related strategies (such as how much of the entire heap memory, how much of the new generation, how much of the old generation, how much Survivor, the conditions for promoting the old generation, etc.)

candidate : For example (-Xmx: set the maximum value of the heap, -Xms: set the initial value of the heap, -Xmn: indicate the size of the young generation, -XX: SurvivorRatio: the ratio of the Eden area to the surviving area, etc.)

candidate : (According to experience: IO-intensive ones can slightly increase the "young generation" space, because most objects will die in the young generation. Memory-computing-intensive ones can slightly reduce the "old generation" space. The space of "year" is larger, and the subject will live longer)

candidate : 2. Garbage collector (choose the appropriate garbage collector, and various tuning parameters of each garbage collector)

candidate : For example (-XX:+UseG1GC: Specify the garbage collector used by the JVM as G1, -XX:MaxGCPauseMillis: Set the target pause time, -XX:InitiatingHeapOccupancyPercent: When the entire heap memory usage reaches a certain percentage, the global concurrency mark The stage will be activated, etc.)

Candidate local conditions, and specific problems are analyzed in detail (the premise is to understand the basic knowledge of JVM, but do not understand the basic knowledge, so how to optimize it)

candidate : In most scenarios, JVM has been able to achieve "out of the box"

Interviewer : Indeed

Candidate : Generally, we do tuning after "encountering problems", and we need to use various "tools" for troubleshooting after encountering problems

candidate : 1. View the "basic" information (process number, main class) of the Java process through the jps command. This command is very commonly used to see how many Java processes are running on the current server, and what are their process numbers and loaded main classes

candidate : 2. Use the jstat command to view information related to the "statistics class" of the Java process (class loading, compilation-related information statistics, GC overview and statistics of each memory area). This command is very often used to see the GC situation

candidate : 3. Use the jinfo command to view and adjust the "running parameters" of the Java process.

candidate : 4. Use the jmap command to view the "memory information" of the Java process. This command is very commonly used to dump JVM memory information to a file, and then use MAT (Memory Analyzer tool) to analyze the file

candidate : 5. View JVM "thread information" through the jstack command. This command uses common phrases to troubleshoot deadlock related issues

candidate : 6. There is also the recently popular Arthas (Ali open source diagnostic tool), which covers the functions of many of the above commands and comes with a graphical interface. This is also my common troubleshooting and analysis tool

Interviewer : Well... okay. When talking about JVM before, you also mentioned that in the "interpretation" stage, there are two ways to interpret bytecode information into machine instruction codes, one is a bytecode interpreter and the other is a just-in-time compiler (JIT)

Interviewer : I would like to ask, do you know the JIT optimization technology of JVM?

Candidate : There are two well-known JIT optimization techniques: method inlining and escape analysis

Candidate : The so-called method inlining is to copy the code of the "target method" into the "method called" to avoid actual method calls

Candidate : Because each method call generates a stack frame (pushing the stack, recording the method call location, etc.), it will bring a certain performance loss, so the optimization of "method inlining" can improve certain performance

Candidate : There are also related parameters for us to specify in the JVM (-XX:MaxFreqInlineSize, -XX:MaxInlineSize)

Candidate : "Escape analysis" is an analysis technique that determines whether an object is referenced by an external method or accessed by an external thread. If it is "not referenced", it can be optimized, for example:

candidate : 1. Lock elimination (synchronization ignore): The object is only accessed inside the method and will not be referenced elsewhere, so it must be thread-safe, and the lock-related code can be ignored

candidate : 2. Allocation on the stack: The object will only be accessed inside the method, and the object is directly allocated in the "stack" (Java defaults to allocate the object in the "heap", which requires the JVM garbage collection period Recycling requires a certain loss of performance, while allocation in the stack is much faster)

Candidate : 3. Scalar replacement/separation object: When the program is actually executed, you can not create this object, but directly create its member variables instead. After the object is split, the member variables of the object can be allocated on the stack or register, and the original object does not need to allocate memory space

candidate : But after so much, different JVM versions have different optimizations for JIT (: here can only be regarded as a reference

Interviewer : Got it.

Suggested reading: [Meituan Technology Blog] Analysis and solution of 9 common CMS GC problems in Java

Welcome to follow my WeChat public [161a81394903fb Java3y ] to talk about Java interviews. The online interviewer series is being updated continuously!

[Online Interviewer-Mobile] The series updated twice a week!

[Online Interviewer-Computer Terminal] The series updated twice a week!

Originality is not easy! ! Seeking three links! !


Java3y
12.9k 声望9.2k 粉丝