A collection of Java performance tuning tips

Source: Internet
Author: User

A collection of Java performance tuning tips

Brother Lian

Resources available for program utilization ( memory,CPU time, network bandwidth, etc. ) is limited, the goal of optimization is to allow the program to complete the scheduled tasks with as few resources as possible. Optimization usually consists of two parts: reducing the volume of the Code and improving the efficiency of the Code. The main discussion in this paper is how to improve the efficiency of code.

I. GENERAL articles

The "general article" discussion issues are appropriate for most Java applications.

1.1 Create an instance of the class without the new keyword

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

when using design mode (Design Pattern) the occasion, if used Factory schema to create the object, use the Clone () method to create a new object instance is very simple. For example, here is A typical implementation of the Factory pattern:

public static credit Getnewcredit ()

{

return new credit ();

}

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

private static Credit Basecredit = new Credits ();

public static credit Getnewcredit ()

{

return (Credit) Basecredit.clone ();

}

The idea above is also useful for array processing.

1.2 using non-blocking I/O

version of the lower The 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 technology can be seen in many applications that must support concurrent I/O flows, such as Web servers, quotes, and auction applications. However, creating a Java thread requires considerable overhead.

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

1.3 caution with exceptions

exceptions are bad for performance. Throwing an exception begins with creating a new object. the constructor for the Throwable interface calls the local (Native) method named fillinstacktrace () , The Fillinstacktrace () method checks the stack and collects the call trace information. Whenever an exception is thrown,theVM 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 for control procedures.

1.4 do not repeat initialization of variables

By default, when you call the class's constructor,JavaThe variable is initialized to a definite value: All objects are set toNULL, Integer variables(Byte, Short,int,long)Set as0,floatand theDoublevariable is set to0.0, the logical value is set tofalse. This is especially important when a class is derived from another class, because theNewWhen a keyword creates an object, all constructors in the constructor chain are called automatically.

1.5 Try to specify the final modifier of the class

A class with a final modifier is not derived. in the Java core API , there are many Examples of final applications, 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 to be final, all methods of that class are final. the Java compiler will look for opportunities for inline (inline) all final methods ( This is related to the specific compiler implementation ). This will increase the performance on average 50%.

1.6 Use local variables as much as possible

parameters that are passed when the method is called and temporary variables created in the call are saved in the stack (Stack) , the speed is faster. Other variables, such as static variables, instance variables, and so on, are created in the heap and are slower. Also, depending on the compiler /JVM, local variables may be further optimized. See Use stack variables whenever possible.

1.7 multiplication and division

Consider the following code:

for (val = 0; Val < 100000; Val +=5)

{

Alterx = val * 8;

Myresult = val * 2;

}

Replacing multiplication operations with shift operations can greatly improve performance. The following is the modified code:

for (val = 0; Val < 100000; val + = 5)

{

Alterx = Val << 3;

Myresult = Val << 1;

}

The modified code does not multiply by 8 , but instead shifts to the equivalent left 3 -bit operation, with each left shift of 1 bits equal to 2 . Accordingly, the right-shift 1 -bit operation is equivalent to 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.


A collection of Java performance tuning tips

Related Article

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.