JAVA program performance optimization

Source: Internet
Author: User

1 Use non-blocking I/O
Earlier JDK versions do not support non-blocking I/O APIs. To avoid I/O congestion, some applications use the method of creating a large number of threads (in good cases, a buffer pool is used ). This technology can be seen in many applications that must support concurrent I/O streams, such as Web servers, quote and auction applications. However, creating a Java thread requires considerable overhead.
JDK 1.4 introduces a non-blocking I/O library (java. nio ). If an application requires JDK of an earlier version, there is a software package that supports non-blocking I/O.
2 Use exceptions with caution
Exceptions are detrimental to performance. To throw an exception, you must first create a new object. The constructor of the Throwable interface calls the local (Native) method named fillInStackTrace (), the fillInStackTrace () method checks the stack, and collects call trace information. As long as an exception is thrown, the VM must adjust the call stack because a new object is created during processing.
Exceptions can only be used for error handling and should not be used to control program processes.
3. Do not reinitialize the variable.
By default, Java initializes the variable to a definite value when calling the class constructor: all objects are set to null, and integer variables (byte, short, int, long) set it to 0, float and double variables to 0.0, and logical value to false. This is especially important when a class is derived from another class, because when an object is created using the new keyword, all constructors in the constructor chain are automatically called.
4. Specify the final modifier of the class whenever possible.
Classes with final modifiers cannot be derived. There are many examples of final applications in the Java core API, such as java. lang. String. Specifying final for the String class prevents people from overwriting the length () method.
In addition, if you specify a class as final, all the methods of this class are final. The Java compiler will look for opportunities to inline (inline) all final methods (this is related to the specific compiler implementation ). This can increase the performance by an average of 50%.
5. Use local variables whenever possible
The parameters passed during method calling and the temporary variables created in the call are saved in the Stack, which is faster. Other variables, such as static variables and instance variables, are created in Heap, which is slow. In addition, local variables may be further optimized depending on the specific compiler/JVM.
 

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.