Basic knowledge
1.1 What is performance?
Before performance tuning, Let's first look at what performance is? With regard to performance, I think everyone who has studied Java can list a few points, or even speak out. In Java TM platform performance, the following five aspects are defined as the criteria for judging performance:
1) performance of operations-which algorithm has the best execution performance?
2) memory allocation-How much memory is required for running the program?
3) Start Time-How long does it take to start the program? This has little impact on WEB projects, but pay attention to the situations when some programs need to be deployed or run on the client (such as Applet programs ).
4) program scalability-what is the performance of a program under pressure?
5) Performance perception-under what circumstances do users think the program is not performing well?
The above five aspects can be judged in specific use cases. As for the performance tuning in these five aspects, the corresponding performance tuning policies will be given in subsequent chapters.
1.2 optimization rules
We only need to focus on the performance issues that affect our programs and can be noticed, rather than every method in every class, we need to find ways to improve performance. If the program performance does not meet our expectation, we need to consider how to optimize the performance. Similarly, although obscure code improves program performance, it may also bring us a nightmare of maintenance. We need to consider the above two situations, so that the code of the program is beautiful and runs fast enough to meet the performance requirements of the customer.
Optimization of the Code may even lead to bad results. Donald knuth (a person with a relatively strong influence, who is the specific person, I forgot, who knows, can tell me, thank you !) Once said, "premature optimization is the root of all edevil ". Before starting performance tuning, you must first point out the reasons for not optimizing the code.
1) if the optimized code is working properly, new bugs may be introduced after optimization;
2) code optimization tends to make the code harder to understand and maintain;
3) code optimized on one platform may be worse on another platform;
4) It takes a lot of time to optimize the code, improving little performance, but leading to obscure code.
Indeed, before optimization, we must seriously consider whether optimization is worthwhile.
1.3 optimization steps
Generally, the following steps are used to improve the performance of an application:
1) Identify application performance indicators and how they meet the expected performance requirements;
2) perform tests on the target platform;
3) if the performance has reached the performance indicator, stop;
4) Find performance bottlenecks;
5) modify the performance bottleneck;
6) return to step 1.
JDK Optimization
2.1 select the appropriate JDK version
JDK of different versions, or even JDK of different manufacturers, may vary greatly, with different performance optimizations. In general, try to select the latest stable JDK version. The latest and stable JDK versions have some bugs and performance optimizations compared with the previous JDK versions.
2.2 Optimization of garbage collection Java heap
Garbage collection is the process of automatically releasing objects that are no longer used by the program. When an object is no longer referenced by a program, its referenced heap space can be recycled to be used by subsequent new objects. The garbage collector must be able to determine which objects are no longer referenced and release the heap space they occupy. If the object is no longer in use, but it is still referenced by the program, it cannot be recycled by the garbage collector. This is the so-called "Memory leakage ". Monitor whether the application has a memory leak. A very good monitoring tool is recommended to you-Quest's JProbe tool to observe the memory changes during the running of the program, you can also generate a memory snapshot to analyze and locate the exact location of the Memory leakage, which can be precisely located in the source code. I will introduce the use of this tool in subsequent chapters.
Java heap refers to the space allocated to the object to survive when the program runs. You can use-mx/-xmx and-MS/-XMS to set the size of the starting heap and the maximum heap. Use-mx and-MS or-xmx and-XMS based on your JDK version and Manufacturer's decision. The Java heap size determines the garbage collection frequency and speed. The larger the Java heap, the lower the garbage collection frequency and the slower the speed. Similarly, the smaller the Java heap, the higher the garbage collection frequency and the faster the speed. To set ideal parameters, you still need to have some basic knowledge.
The maximum value of the Java heap cannot be too large, which may cause frequent swap and paging of the system memory. Therefore, the maximum memory must be lower than the physical memory minus the memory required by other applications and processes. In addition, the heap setting is too large, resulting in a long garbage collection time, which will not be worth the candle, greatly affecting the program performance. The following are some frequently used parameter settings:
1) Set-XMS to the value of-xmx;
2) estimate the space occupied by the living objects in the memory. Set-XMS to be equal to this value, and-xmx to four times this value;
3) Set-XMS to 1/2 of-xmx;
4) Set-XMS between-1/10 and 1/4 of xmx;
5) use the default settings.
You need to determine the most suitable parameter settings based on the specific application scenarios of your running program.
In addition to the two most important parameters-XMS and-xmx, there are also many parameters that may be used. These parameters are usually strongly dependent on the garbage collection algorithm, the JDK version may be different from that of the manufacturer. However, these parameters are rarely used in web development, so I will not detail them. In actual applications, pay attention to setting-XMS and-xmx to optimize the application as much as possible. For programs with high performance requirements, you need to study the mechanism of Java Virtual Machine and garbage collection algorithm. Let's take a look at the book deep Java Virtual Machine translated by Cao Xiaogang.