Java Performance Optimization _ reprint

Source: Internet
Author: User
Tags finally block

First, avoid using complex expressions in cyclic conditions
1, in the case of No compilation optimization, in the loop, the loop condition will be repeated calculation, if you do not use complex expressions, and so that the cyclic condition value is not changed, the program will run faster.

2, can not use multi-layered nested.

Second, set size
When the JVM expands the size of the vector, it needs to recreate a larger array, copy the contents of the original array, and finally, the original array is recycled. The expansion of vector capacity is a time-consuming matter.
Usually, the default size of 10 elements is not enough, you'd better be able to accurately estimate the optimal size you need.

Array arrays are the most efficient, but the capacity is fixed and cannot be changed dynamically, ArrayList capacity can grow dynamically, but at the expense of efficiency.

single thread should try to use HashMap, ArrayList, unless necessary, it is not recommended to use Hashtable,vector, they use the synchronization mechanism, and reduce performance.

Third, close the stream in the finally block

The resources used in the program should be freed to avoid resource leaks. This is best done in the finally block. The finally block is always executed, regardless of the result of the program execution, to ensure that the resource is closed correctly.

database connection, I/O flow operation, after the use is complete, close to release resources in a timely manner. Because the operation of these large objects can cause a large overhead on the system.

Iv. using ' system.arraycopy () ' Instead of iterating through the array
' System.arraycopy () ' is much faster than copying an array by looping.

V. Let the Getter/setter method of accessing the variables in the instance become "final"
The simple Getter/setter method should be set to final, which tells the compiler that this method will not be overloaded, so it can become "inlined"

Vi. do not call the synchronized (synchronous) method in a loop
Method synchronization requires a considerable amount of resources, and calling it in a loop is definitely not a good idea.

It is best to nest loops inside the synchronization.

Move the Try/catch block out of the loop
Putting the Try/catch block into the loop will greatly affect performance, and if the JIT is turned off or you are using a JVM without a JIT, performance will degrade

Viii. do not instantiate variables in the loop body
Instantiating a temporary variable in the loop body increases memory consumption.

It is best to define variables outside the loop body and use them repeatedly.

Nine, use the stack variable as much as possible
If a variable needs to be accessed frequently, then you need to consider the scope of the variable. Static? Local or instance variable? Accessing static and instance variables will cost 2-3 more clock cycles than accessing local variables.

If possible, use local variables as variables that you frequently access.

10. Do not repeat the initialization of variables
By default, when the constructor of a class is called, Java initializes the variable to a certain value, all objects are set to NULL, the integer variable is set to 0,float and the double variable is set to 0.0, and the logical value is set to False. This is especially important when a class is derived from another class, because all constructors in the constructor chain are called automatically when an object is created with the new keyword.
Here's a note that when you set an initial value for a member variable but need to call another method, it's best to put it in a method such as initxxx (), because calling a method assignment might throw a null pointer exception because the class has not yet been initialized, public int state = This.getstate ( );

XI. Traversal of Haspmap

map<string, string[]> paramap = new hashmap<string, string[]> (); for (entry<string, String[]> Entry:pa Ramap.entryset ()) {    String Appfielddefid = Entry.getkey ();    String[] values = Entry.getvalue ();}

using the hash value to take out the corresponding entry to compare the results, obtain the value of entry directly after the key and value.

12. The difference between stringbuffer,stringbuilder is that Java.lang.StringBuffer is a thread-safe variable character sequence. A string-like buffer, but cannot be modified. StringBuilder generally should prefer the StringBuilder class when compared to this class, because she supports all the same operations, but is faster because she does not perform synchronization. For better performance, you should try to specify her capacity when constructing StringBuffer or StringBuilder. Of course, no more than 16 characters.
In the same situation, using StringBuilder can only get 10%~15% performance gains compared to using stringbuffer, but it risks a multi-thread insecurity. It is recommended that stringbuffer be used in a comprehensive consideration.

Java Performance Optimization _ reprint

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.