A basic knowledge
1.1 What is performance
Before performance tuning, let's start by understanding what performance is. With regard to performance, I think everyone who has studied Java can list a few points and even talk about it. In the Java TM Platform performance, the following five aspects are defined as criteria for judging performance:
1) The performance of the operation--which algorithm performs the best performance?
2) Memory allocation-How much memory is required to run the program?
3) Startup time-how long does it take to start the program? This has little impact on web projects, but be aware of situations where some programs need to be deployed or run on the client side (such as applet programs).
4) Scalability of the program-how does the program perform under pressure loads?
5) Performance perception-under what circumstances will the user feel that the performance of the program is not good?
The above five aspects, in the specific use scene can have the choice to judge. As for these five aspects of performance tuning, in subsequent chapters will be given to the corresponding performance tuning strategy.
1.2 Rules for tuning
We just need to be concerned about the performance issues that affect our programs, and not every method in every class we need to improve performance. If the performance of the program does not meet the requirements we expect, we need to consider how to optimize performance. Similarly, the obscure code, while improving the performance of the program, may give us a maintenance nightmare. We need to compromise the two scenarios, making the code of the program graceful and running fast enough to meet the desired performance requirements of the customer.
Optimization code can even lead to undesirable results, Donald Knuth (a more influential person compared to the cow, specifically who, I also forgot, who knows, can tell me, thank you!) ) once said, "Premature optimization is the root of all evil". Before you start performance tuning, you need to point out some reasons for not optimizing your code.
1) If the optimized code is working properly, a new bug may be introduced after optimization;
2) Optimized code tends to make code more difficult to understand and maintain;
3) optimized code on one platform may be worse on another platform;
4) spend a lot of time on the optimization of the code, improve a little performance, but led to obscure code. Indeed, before optimization, we must carefully consider whether it is worthwhile to optimize.
1.3 Tuning Steps
Generally we improve the performance of the application is divided into the following several steps:
1) Define the application performance indicators, how to meet the desired performance requirements;
2) test on the target platform;
3) If performance has reached performance indicators, Stop;
4) Find performance bottlenecks;
5) Modify the performance bottleneck;
6) Return to step 2nd.
Two JDK tuning
2.1 Choosing the right JDK version
Different versions of the JDK, or even the JDK of different manufacturers, are likely to differ greatly in the degree of performance optimization. In general, choose the latest released stable JDK version as much as possible. The latest stable JDK version will do some bug modification and performance optimization relative to the previous JDK version.
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 the program, the heap space it refers to can be recycled for use by subsequent new objects. The garbage collector must be able to determine which objects are no longer referenced and can release the heap space they occupy. If the object is no longer being used, but is also referenced by the program, it cannot be reclaimed by the garbage collector, which is called a "memory leak". Monitoring the application for memory leaks, there is a very good monitoring tool recommended to everyone--quest Company's Jprobe tool, use it to observe the program runtime memory changes, and can generate memory snapshots, so as to analyze and locate the exact location of the memory leak, can be accurately located in the source code. The use of this tool will be described in the following chapters.
The Java heap is the space that is allocated to the object's survival when the program runs. The size of the starting heap and the maximum heap size are set by-mx/-xmx and-MS/-XMS. Use-MX and-ms or-xmx and-xms as per your JDK version and manufacturer's decision. Java heap size determines the frequency and speed of garbage collection, the larger the Java heap, the lower the frequency of garbage collection, the slower the speed. Similarly, the smaller the Java heap, the higher the frequency of garbage collection, and the faster it gets. To set the ideal parameters, you still need to know some basic knowledge. The maximum value of the Java heap cannot be too large, which can cause system memory to be exchanged and paged frequently. Therefore, the maximum memory must be less than the physical memory minus the memory required by other applications and processes. and heap settings too large, resulting in garbage collection time is too long, this will not outweigh the loss, greatly affect the performance of the program. Here are some of the frequently used parameter settings:
1) Set the value of-xms equal to-XMX;
2) estimate the size of the space occupied by the surviving objects in memory, set-XMS equal to this value,-xmx four times times the value;
3) Set-xms equal to-xmx 1/2 size;
4) Set the-xms between 1/10 and 1/4-xmx;
5) Use the default settings.
You need to determine the most appropriate parameter settings for your own running program based on the specific usage scenario. In addition to the two most important parameters of-XMS and-xmx, there are a number of parameters that are likely to be used, which are often strongly dependent on garbage collection algorithms, and may vary depending on the version of the JDK and the manufacturer. But these parameters are generally used in web development less, I do not do a detailed introduction. In practical applications, it is important to set-XMS and-xmx so that they can optimize the application as much as possible. For applications with high performance requirements, it is necessary to study the mechanism of Java Virtual machine and garbage collection algorithm.
Basic knowledge of Java Program Performance Tuning and JDK tuning