Java application performance optimization

Java application performance optimization problem is a programmer must consider typical performance issues such as slow page response, the interface timeout, high server load, low number of concurrent database frequent deadlocks.

Java application performance bottleneck point is very large, systemic factors such as disk, memory, network I / O, etc., Java application code, JVM GC, databases, caching. Java performance optimization can be divided into four levels: the application layer, a database layer, a frame layer, the JVM layer, as shown in FIG.

FIG hierarchical model performance optimization 1.Java

Java application performance optimization

Each layer optimization increasingly hard, and knowledge to solve the problems involved will be different. For example, the application logic layer needs to understand the code, locate the line of code in question by the Java thread stack, and so on; the database level need to analyze the SQL, positioning deadlock; layer need to understand the source code framework, a framework for understanding the mechanism; JVM level and type of work required for the GC mechanisms have a good understanding of the role of various parameters JVM gains.

1 CPU diagnosis

The main concern for the CPU average load (Load Average), CPU usage, context switches (Context Switch).

Can view the system load and average CPU usage by the top command, FIG. 2 is a top view of a system through a command status.

2.top command example of FIG.
Java application performance optimization

Average load of three numbers: 63.66,58.39,57.18, respectively, over 1 minute, 5 minutes, 15 minutes of machine load. According to experience, if the value is less than 0.7 * number of CPU, the system is working properly; if more than this value, even up to four or five times the number of CPU cores, the load on the system is significantly higher.

In Figure 2 the load has reached 15 minutes 57.18,1 load is 63.66 minutes (for the 16-core system), the system described loading problems arise, and the present tendency is further increased, the need to locate specific reasons.

Can be viewed by the CPU context switching times vmstat command:

Scene number of occurrences of context switches have the following main categories:

• 1) time slice expires, CPU normal task scheduling;

• 2) are other higher priority task preemption;

• 3) perform tasks encountered I / O obstruction, suspend the current task, a task switch to the next;

• 4) user code initiative suspend the current task to give up CPU;

• 5) preemptive multitasking resources, because there is no grab is suspended;

• 6) hardware interrupt.

Java thread context switching mainly from the shared resource competition. Generally a single object locking system rarely become a bottleneck, unless the lock granularity is too large. However, an access frequency is high, a plurality of successive objects in a locked block of code may appear a large number of context switches, become a system bottleneck.

For example, once in our system log4j 1.x volume printing at larger concurrent logs, frequent context switching, a lot of thread blocking, lead to a large drop in system throughput, which is the relevant code is shown in Listing 1, to upgrade log4j 2.x only solve this problem.

Java application performance optimization

2 Memory

From the perspective of the operating system, memory, attention to the adequacy of the application process, you can use the free -m command to view memory usage.

You can view the process used by the top command virtual memory VIRT and physical memory RES, according to the formula VIRT = SWAP + RES can calculate the swap partition specific application used (Swap), the use of swap too much effect on Java application performance can be swappiness transferred to the value as small as possible.

Because for Java applications, take up too much swap can affect performance, disk performance, after all, much slower than memory.

. 3 I / O
I / O includes a disk I / O, and network I / O, disk more prone to I / O bottlenecks in general. Can see how disk read by iostat, through the CPU I O wait can be seen / disk I / O is normal.

If the disk I / O has been in a high state, indicating slow or disk failure, it has become a performance bottleneck, the need for application optimization or disk replacement.

4 Java application and diagnostic tool

The application code is relatively easy to solve performance problems of a class of performance problems. Monitoring through some application level alarm (UMP), if there are problems and code determined by the code can be located directly; or via top + jstack, identify problem thread stack, positioned on issues threaded code, can be problem found. For more complex, logic more code segments, most often the application code may be positioned Stopwatch performance problems by printing performance log.

Common Java application diagnostics including diagnostic thread stack, GC and other aspects.

jstack

jstack with the top command is typically used Java localization process and thread through the top -H -p pid, reuse jstack -l pid export thread stack. As the thread stack is transient, thus requiring multiple dump, usually three times a dump, usually every every 5s on the line. The positioning of the top Java thread pid turn into hexadecimal, get Java thread stack nid, the problem can be found in the corresponding thread stack.

JProfiler

JProfiler can be analyzed on the CPU, heap, memory, and powerful.

Like the author of this article can give a point endorsed, look, will share Java-related articles every day! There are benefits presented from time to time, including the consolidation of learning materials, interview questions, such as source code ~ ~

Guess you like

Origin blog.51cto.com/14440597/2424368