After the mid-1990s, Java has also attracted some criticisms while winning. Winner is mainly the Java's cross-platform operability, that is, the so-called "Write Once, Run Anywhere". However, due to the performance of Java, there is still a lot of gaps compared to the Running efficiency, there is a lot of gaps. criticism.
For the application of the server, the performance problem of Java seems to be unclear, so that some Java technology, such as JSP, Servlet, EJB, etc. are very obvious, so that some JAVA technology, such as JSP, servlet, ejb, etc. Large application, but in fact, Java's performance issues still exists on the server. Below I will discuss the performance and implementation efficiency of Java and improve Java performance.
One. Basic knowledge about performance
1. Definition of performance
We need to understand the true meaning of "Performance" before we discuss how to improve Java performance. We generally define the following five aspects as the criteria for judging performance.
1) Performance of the operation - - Which algorithm is performed in performance performance?
2) The allocation of memory ---- The program needs to assign how much memory, the efficiency and performance of the runtime.
3) Start time ---- How much time it takes to start.
4) The scalability of the program ----- the performance of the program in the case of user load.
5) Performance's understanding ------ How can users recognize the performance of the program.
For different applications, the requirements for performance are also different. For example, most applications take longer time at startup, thereby decreasing the starting time; the application of the server is usually allocated with large memory space, so the requirements for memory are also reduced. However, this is not that the performance of the two aspects can be ignored. Second, the performance of the algorithm is very important for applications that use business logic to transactional operations. In general, the requirements for the application will determine the priority to each performance.
2. How can I improve the performance of Java?
Improve Java performance, generally considering the following four main aspects:
(1) Method and mode for programming
A good design can improve the performance of the program, which is not only suitable for Java, but also any programming language. Because it makes full use of various resources, such as memory, CPU, cache, object buffer pools and multithreades, thereby designing high performance and scalable systems.
Of course, in order to improve the performance of the program, it is more difficult to change the original design, but the importance of program performance is often higher than the design of the design. Therefore, there should be a good design model and method before the programming begins.
(2) Environment of Java deployment.
The environment of Java deployment refers to techniques for explaining and executing Java bytecles, usually as follows. That interpretation INSTRUCTIONS (Interpreter Technology), Technical time compiler (Just In Time Compilier Technology), adaptive optimization technique (Adaptive Optimization Technology), dynamic optimization, machine code is compiled ahead technique (Dynamic Optimization, Ahead Of Time Technology) And compile as machine code (Translator Technology).
These technologies are generally optimized by optimizing thread models, adjusting the size of the stack and stack to optimize Java performance. When considering the improvement of Java performance, you must first find bottlenenecks that affect Java performance. After confirming the rationality of the design, the Java deployment environment should be adjusted to improve the performance of the Java application by changing some parameters. See Section 2 of the specific content. (3) Realization of Java applications
When discussing the performance of the application, most of the programmers considerate the code of the program, which is of course right, when more important is to find bottleneck code affecting the program performance. In order to find these bottleneck code, we generally use some auxiliary tools, such as JProbe, OptimizIT, VTune, and some analyzed tools such as Towerj Performance. These assisted tools can track the time consumed by each function or method in the application, thereby improving the performance of the program.
(4) Hardware and operating systems
In order to improve the performance of the Java application, use Fast CPUs and more memory, and believe that this is the only way to improve program performance, but this is not the case. Practical experience and facts have proven that only the application performance bottlenecks, there is appropriate way, such as design patterns, deployment, and operating system adjustments, is the most effective.
3. The usual performance bottleneck in the program.
All applications have performance bottlenecks. In order to improve the performance of the application, it is necessary to reduce the bottleneck of the program as much as possible. The following is a performance bottleneck that often exists in the Java program.
After you understand these bottlenecks, you can reduce these bottlenecks, thereby improving the performance of Java applications.
4. Steps to improve the performance of the Java program
In order to improve the performance of the Java program, the following six steps are needed.
a) Clear specific requirements for performance
Before implementing a project, you must clarify the specific requirements for program performance, such as this application to support 5000 concurrent users, and the response time is within 5 seconds. But it also understands that the requirements for performance should not conflict with other requirements of the program.
b) Understand the performance of the current program
You should understand the performance of your application with the gap between the performance requirements of the project. The usual indicator is the number of processing and response time in unit time, and sometimes the utilization of CPU and memory is compared.
c) Find the performance bottleneck of the program
In order to find out the performance bottleneck in the program, some analytics tools are usually used, such as: TowerJ Application Performance Analyzer or VTune to see and analyze the time consumption time of each element in the program stack, thus correctly find and correct the bottleneck code that causes performance reduced. Improve the performance of the program. These tools can also find potential issues such as excessive abnormalities, garbage collection.
d) take appropriate measures to improve performance
After finding the bottleneck code that caused the program performance, we can use the four aspects of the improvement in the previously introduced, namely design patterns, Java code implementations, deployment Java environment and operating systems to improve the performance of the application. The details will be described in detail in the following.
e) Improve performance only on certain aspects of modification
Once only change, only one aspect of the performance can be reduced, then the performance of the program is improved, and it should not change multiple aspects at a time, because you will not know which aspect change improves the performance of the program, which aspect No, you can't know where the program bottleneck is.
f) Return to step c, continue to work similar to the performance, which has been achieved.
two. Java deployment environment and compilation technology
When developing a Java application, first compile the Java source program into a byte code that is not related to the platform. These bytes can be performed by various JVM-based technologies. These technologies are mainly divided into two large categories. That is, a technique based on the interpretation and technique based on the pre-compiled as local code. The schematic is as follows: specifically divided into the following five categories:
a) explain the instruction technology
The structural diagrams and execution processes are as follows:
Java's compiler first compiles the Java source file as a bytecode. These bytes are the Java Virtual Machine (JVM) is the machine's instruction code. Then, Java's interpreter continuous loops takes out the bytecode and executes.
The advantage of this is that the cross-platform of the Java language can be implemented, and the generated bytecode is also compacted. Some of Java, such as security, dynamics have to be kept; but the disadvantage is that the province's generated bytecode is not optimized, and the speed is slower than all compiled local code.
b) Compilation Technology in time (Just In Time)
Timely compilation technology is to solve the instruction interpretation technology is relatively low, and the speed is relatively slow, and the structural diagram is shown below.
Its main change is to compile JAVA's bytecode as machine code before the Java program is executed. Thereby directly executing the machine code directly at runtime without using the bytecode. At the same time, the code also has been partially optimized.
The advantage of this is that it greatly improves the performance of the Java program. At the same time, since the results of the compilation are not stored in the program running, the time to store the loader is also saved; the disadvantage is that the JIT compiler is optimized to all the code, so it is also a lot of time.
IBM and Sun provide relevant JIT products.
c) Adaptive Optimization Technology (Adaptive Optimization Technology)
Adaptive optimization techniques are not optimized for all bytecods compared to JIT technology. It tracks the procedure of the program run, thus discovering the code that needs to be optimized, and dynamically optimized the code. For optimized code, take 80/20 strategies. In theory, the longer the program is running, the more optimized the code. The structural diagram is as follows:
The advantage is that adaptive optimization techniques take advantage of the information execution time, the performance bottleneck of the issuer, thereby increasing the performance of the program; its disadvantage is that improper selection when optimizing, and reduces the performance of the program.
Its main products are IBM, and SUN's Hotspot.
d) Dynamic optimization, compile as machine code in advance (Dynamic Optimization, Ahead of Time)
Dynamic optimization technology fully utilizes Java source code compilation, bytecode compile, dynamic compilation, and static compilation techniques. The original code or bytecode of Java is input, and the output is a mixture of highly optimized executable code and a dynamic library (a DLL file in Window, and the shared library in Unix is a shared library .a .so file). The structure is as follows:
Its advantage is to greatly improve the performance of the program; the disadvantage is to destroy Java's portability and bring a certain hidden danger for Java's security.
Its main product is TowerJ3.0.