Java performance optimization (top)

Source: Internet
Author: User
Tags exception handling execution garbage collection advantage
Performance | Optimization Java in the mid-90, after the emergence of praise, but also attracted some criticism. The winning admiration is mainly Java's cross-platform operability, the so-called "Write Once,run Anywhere". But because Java performance and operational efficiency compared with C, there is still a large gap, which attracted a lot of criticism.
For server-side applications, due to the lack of interface design and frequent restart of the program, Java performance problems appear to be less obvious, so some Java technology, such as JSP, SERVLET,EJB, etc. in the server-side programming has been a great application, but in fact, Java performance problems still exist on the server side. Here are four ways to discuss the performance and execution efficiencies of Java and some ways to improve Java performance.
A Basic knowledge of performance
1. Definition of performance
Before we discuss how to improve the performance of Java, we need to understand what "performance" really means. We generally define the following five aspects as criteria for judging performance.
1 performance of the operation----which algorithm is the best performing performance
2 The allocation of memory----How much memory the program needs to allocate, the most efficient and performance at run time.
3 Startup time----How long it takes to start the program.
4 The scalability of the program-----Program in the case of heavy user load performance.
5 understanding of the performance------how users can realize the performance of the program.
For different applications, performance requirements are different. For example, most applications require a long time to start up, which reduces startup time requirements, and server-side applications typically have large memory space allocated, so the memory requirements are reduced. However, this is not the performance of these two aspects can be ignored. Second, the performance of algorithms is important for applications that apply business logic to transactional operations. Overall, the requirements for the application will determine the priority for each performance.
2. How to improve the performance of Java
To improve Java performance, generally consider the following four main areas:
(1) Methods and modes of programming
A good design can improve the performance of the program, not only for Java, but also for any programming language. Because it makes full use of various resources, such as memory, CPU, cache, object buffer pool and multithreading, so as to design high-performance and scalable system.
Of course, it is difficult to change the original design in order to improve the performance of the program, but the importance of program performance is often higher than the design changes. Therefore, there should be a good design model and method before programming begins.
(2) The environment of the Java Cloth Department.
The Java layout environment is the technology used to interpret and execute Java bytecode, typically five of these. That is, interpreting instruction technology (interpreter Technology), Just-in-time-compiled technology (Just in time Compilier Technology), Adaptive Optimization Technology (adaptive Optimization), Dynamic optimization, a technology that compiles early into machine code (dynamic Optimization,ahead of Time Technology) and a technology compiled to machine code (Translator Technology).
These technologies typically optimize the Java performance by optimizing the threading model and resizing the heap and stack. When considering improving Java performance, first find bottlenecks that affect Java performance (bottlenecks), and after confirming the rationality of the design, you should adjust the Java environment and improve the performance of your Java application by changing some parameters. See section II for specific content.
(3) Implementation of Java application
When discussing the performance of an application, most programmers will consider the code of the program, which is certainly true, when it is more important to find the bottleneck code that affects the performance of the program. In order to find these bottleneck codes, we usually use some auxiliary tools, such as jprobe,optimizit,vtune and some analytical tools such as TOWERJ performance. These assistive tools can improve the performance of your program by tracking the time it takes to execute each function or method in your application.
(4) Hardware and operating system
In order to improve the performance of Java applications, with faster CPU and more memory, and that this is the only way to improve program performance, but this is not the case. Practical experience and facts prove that only by the bottleneck of application performance, the appropriate methods, such as design patterns, the layout of the environment, operating system adjustment, is the most effective.
3. A typical performance bottleneck in a program.
All applications have performance bottlenecks, and in order to improve the performance of the application, reduce the bottleneck of the program as much as possible. The following are performance bottlenecks that are common in Java programs.


Once these bottlenecks are understood, they can be targeted to reduce these bottlenecks, thereby improving the performance of Java applications
4. Steps to improve Java program performance
To improve the performance of your Java program, you need to follow the following six steps.
A identify specific requirements for performance
Before implementing a project, you must identify the specific requirements of the project for program performance, such as: The application supports 5,000 concurrent users, and the response time is within 5 seconds. But also understand that the requirements for performance should not conflict with other requirements for the program.
b Understand the performance of the current program
You should understand the gap between the performance of your application and the performance required by your project. The usual metrics are the number of processing and response times per unit of time, and sometimes the CPU and memory utilization.
c) Find the performance bottleneck of the program
To discover performance bottlenecks in a program, you typically use profiling tools such as TOWERJ Application Performance Analyzer or VTune to view and analyze the time that each element in the program stack consumes. To correctly find and correct the bottleneck code that causes performance degradation, thereby improving the performance of the program. These tools can also uncover potential problems such as excessive exception handling, garbage collection, and more.
D) Take appropriate measures to improve performance
After finding the bottleneck code that caused the performance of the program to degrade, we can improve the performance of the application by using the four aspects of performance improvement described earlier, namely design pattern, Java code implementation, and the layout of the Java environment and operating system. The specifics will be described in detail in the following sections.
E to improve performance by modifying only one aspect
Change only one aspect of the performance degradation at a time, and then observe whether the program's performance has improved, rather than changing multiple aspects at once, because you will not know exactly which aspect of the change improves the performance of the program, which aspect does not know where the program bottleneck is.
f) Go back to step c and continue to do similar work until the required performance has been achieved.

Two Java-Layout Environment and compilation technology
When developing a Java application, the Java source program is first compiled into a platform-independent byte code. These bytecode can be executed by a variety of JVM based technologies. These technologies are mainly divided into two major categories. The technology based on interpretation and the technology based on early compilation to local code. The schematic diagram is as follows:


Can be divided into the following five categories:
A) Interpreting instruction techniques
Its structure diagram and execution process are as follows:


The Java compiler first compiles the Java source files into bytecode. These bytecode are the machine's script for the Java Virtual Machine (JVM). The Java interpreter then loops through the byte code to interpret and execute.
The advantage of this is that the Java language can be implemented across platforms, while the resulting bytecode is also more compact. Some of the advantages of Java, such as security, dynamic are maintained, but the disadvantage is that the province generated bytecode has not been optimized, compared with all compiled local code, the speed is relatively slow.
(b) Just-in-time compilation technology (Just in time)
In order to solve the problem that the instruction interpretation technique is low in efficiency and the speed is slow, the structure diagram is shown below.


The main change is that the JIT compiler compiles the Java bytecode into machine code before the Java program executes. This will execute the machine code directly while the program is running, without having to interpret the bytecode. At the same time, the code is also part of the optimization.
The advantage of this is that it greatly improves the performance of the Java program. At the same time, because the results of the compilation is not stored between the program, it also saves the time to load the program; The disadvantage is that because the JIT compiler wants to optimize all the code, it also wastes a lot of time.
Both IBM and Sun provide related JIT products.
(c) Adaptive optimization technology (Adaptive Optimization Technology)
Compared with JIT technology, adaptive optimization technology does not optimize all bytecode. It tracks the process of running a program, discovering code that needs to be optimized, and dynamically optimizing the code. For optimized code, take a 80/20 strategy. Theoretically, the longer the program runs, the better the code will be. The structure of the chart is as follows:


The advantage is that the adaptive optimization technology makes full use of the information of program execution, the bottleneck of the performance of the release program, and improves the performance of the program. The disadvantage is that the optimization may be improper, and the performance of the program can be reduced.
Its main products are also Ibm,sun hotspot.
d dynamic Optimization, early compilation of machine code technology (dynamically optimization,ahead of Time)
Dynamic optimization technology Fully utilizes Java source code compiling, bytecode compiling, dynamic compiling and static compiling technology. The Java source code or bytecode when it is entered, and the output is a mixture of highly optimized executable code and a dynamic library (DLL files in Windows, shared Libraries in Unix. A. so file). The structure is as follows:


The advantage is that it can greatly improve the performance of the program, the disadvantage is to destroy the portability of Java, but also to the security of Java has brought some hidden dangers.
Its main product is TowerJ3.0.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.