Java Performance Optimization general article

Source: Internet
Author: User
Tags constructor error handling final interface modifier variables requires variable
Performance | Optimization One, general article

The issues discussed in "common article" are appropriate for most Java applications.


1.1 Create an instance of a class without the new keyword


When you create an instance of a class with the new keyword, all constructors in the chain of the constructor are invoked automatically. But if an object implements the Cloneable interface, we can call its clone () method. The Clone () method does not call any class constructors.


In situations where design patterns are used, if you create objects in Factory mode, it is simple to use the Clone () method instead to create a new object instance. For example, the following is a typical implementation of the factory pattern:


public static Credit Getnewcredit () {Return to new Credit ();}




The improved code uses the Clone () method, as follows:


private static Credit Basecredit = new Credit ();p ublic static Getnewcredit () {return (Credit) Basecredit.clone ();}





The above ideas are also useful for array processing.


1.2 Using non-blocking I/O


The less-than-version JDK does not support non-blocking I/O APIs. To avoid I/O blocking, some applications use a method of creating a large number of threads (in better cases, a buffer pool is used). This technique can be seen in a number of applications that must support concurrent I/O flows, such as Web servers, quotes, and auction applications. However, creating a Java thread requires considerable overhead.


JDK 1.4 introduces a non-blocking I/O library (Java.nio). If the application requires the use of an older JDK, there is a package that supports non-blocking I/O.


1.3 Cautious use of abnormal


Exceptions are bad for performance. Throwing an exception begins by creating a new object. The constructor of the Throwable interface calls the local (Native) method named Fillinstacktrace (), and the Fillinstacktrace () method checks the stack to collect call tracking information. Whenever an exception is thrown, the VM must adjust the call stack because a new object is created during the process.


Exceptions can only be used for error handling and should not be used to control the process.


1.4 Do not repeat initialization of variables


By default, when the constructor of a class is invoked, Java initializes the variable to the determined value: All objects are set to NULL, the integer variable (byte, short, int, long) is set to 0,float and the double variable is set to 0.0, and the logical value is set to False. This should be especially noted when a class derives from another class, because when an object is created with the new keyword, all constructors in the chain of the constructor are invoked automatically.


1.5 Specify the final modifier for the class as much as possible


A class with the final modifier is not derived. In the Java Core API, there are many examples of final application, such as java.lang.String. Specifying final for the string class prevents people from overwriting the length () method.


In addition, if you specify that a class is final, all methods of that class are final. The Java compiler looks for the opportunity to inline (inline) all final methods (this is related to the specific compiler implementation). This will increase performance by an average of 50%.


1.6 Use local variables as much as possible


The parameters passed when the method is invoked and the temporary variables created in the call are saved in the stack (stack) faster. Other variables, such as static variables, instance variables, and so on, are created in the heap (Heap) in a slower speed. In addition, local variables may be further optimized depending on the specific compiler/JVM.


1.7 Multiplication and division


Consider the following code:


for (val = 0; Val < 100000 Val +=5) {Alterx = val * 8; myresult = val * 2;}




Using shift operations to replace multiplication operations can greatly improve performance. Here is the modified code:


for (val = 0; Val < 100000; Val + 5) {Alterx = Val << 3; myresult = Val << 1;}





The modified code is no longer multiplied by 8, instead using an equivalent left 3-bit operation, with 1 bits per left being multiplied by 2. Accordingly, the right 1-bit operation is the equivalent of dividing by 2. It is worth mentioning that, although the shift operation is fast, but may make the code more difficult to understand, so it is best to add some comments.




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.