Summary of 35 Java code performance optimizations 10-20

Source: Internet
Author: User
Tags object object sessions


11. Multiplication and division using shift operations

For example:

for (val = 0; Val < 100000; val + = 5) {a = Val * 8; b = VAL/2;}

The shift operation can greatly improve performance, because at the bottom of the computer, the bitwise operation is the most convenient and fastest, so it is recommended to modify:

for (val = 0; Val < 100000; val + = 5) {a = Val << 3; b = Val >> 1;}

Although the shift operation is fast, it may make the code less understandable, so it's best to add the appropriate comments.


12. Do not constantly create object references within the loop

For example:

for (int i = 1; I <= count; i++) {Object obj = new Object ();}

This practice causes memory to count as the object reference exists, and if count is large, it consumes memory and is recommended instead:

Object obj = null;for (int i = 0; I <= count; i++) {obj = new Object ();}

In this case, only one copy of the object object is referenced in memory, and each time new object () is used, the object reference points to a different object, but only one copy in memory, which saves memory space considerably.


13, based on the efficiency and type checking considerations, should use as far as possible array, cannot determine the size of the array to use ArrayList


14, try to use HashMap, ArrayList, StringBuilder, unless the thread security needs, it is not recommended to use Hashtable, Vector, StringBuffer, the latter three due to the use of synchronization mechanism resulting in performance costs

15. Do not declare the array as public static final

Because this is meaningless, it simply defines the reference as static final, the contents of the array can be arbitrarily changed, and declaring the array public is a security vulnerability, which means that the array can be changed by the outer class


16, try to use a single case in the appropriate situation

Using a single example can reduce the load burden, shorten the load time, improve the efficiency of loading, but not all places are applicable to a single case, simply speaking, the single case is mainly applicable to the following three aspects:

(1) Control the use of resources, through thread synchronization to control the concurrent access of resources

(2) control of the production of instances to achieve the purpose of saving resources

(3) Controlling the sharing of data, allowing multiple unrelated processes or threads to communicate without establishing a direct association


17, try to avoid arbitrary use of static variables

You know, when an object is referenced by a variable defined as static, the GC usually does not reclaim the heap memory that the object occupies, such as:

public class A {private static b b = new B ();}

At this point, the life cycle of static variable B is the same as Class A, and if Class A is not unloaded, the B object referred to by B will reside in memory until the program terminates


18. Timely removal of sessions that are no longer needed

In order to clear a session that is no longer active, many application servers have a default session timeout, which typically takes 30 minutes. When the application server needs to save more sessions, if there is not enough memory, the operating system will transfer some of the data to disk, and the application server may dump partially inactive sessions to disk based on the MRU (most recently used) algorithm, and may even throw out-of-memory exceptions. If a session is to be dumped to disk, it must be serialized first, and the cost of serializing the object is expensive in a large-scale cluster. Therefore, when the session is no longer needed, the httpsession invalidate () method should be called in time to clear the session.


19, the implementation of the Randomaccess interface, such as ArrayList, should use the most common for loop instead of the Foreach Loop to traverse

This is what the JDK recommends to the user. The JDK API's explanation for the Randomaccess interface is that the implementation of the Randomaccess interface is used to indicate that it supports fast random access, and the primary purpose of this interface is to allow a generic algorithm to change its behavior so that it can provide good performance when applied to random or contiguous access lists. The actual experience shows that the class instance that implements the Randomaccess interface, if it is random access, uses the normal for loop efficiency higher than the Foreach loop, and conversely, if it is accessed sequentially, the use of iterator is more efficient. You can use code like this to make a decision:

if (list instanceof randomaccess) {for (int i = 0; i < list.size (); i++) {}}else{iterator<?> Iterator = List    . iterable (); while (Iterator.hasnext ()) {Iterator.next ()}}

The underlying implementation of the Foreach loop is the iterator iterator, see Java Syntax sugar 1: variable length parameters and the Foreach Loop principle. So the latter half of the sentence "in turn, if it is sequential access, the use of iterator will be more efficient" means that sequential access to those class instances, using the Foreach Loop to traverse.


20. Use synchronous code block instead of synchronization method

This is clearly stated in the synchronized lock method block in multithreaded modules, unless you can determine that a whole method needs to be synchronized, try to use synchronous blocks of code, avoid synchronizing code that does not need to be synchronized, and affect the efficiency of code execution.

This article is from the "Long Guangxiang blog," Please make sure to keep this source http://18073491002lgx.blog.51cto.com/12044386/1876609

Summary of 35 Java code performance optimizations 10-20

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.