1. For classes that do not need to be derived clearly, add the final modifier. In this case, all the methods of this class are final. The Java compiler looks for opportunities to inline all final methods. (Improves performance by 50%)
2. Try to reuse objects to avoid generating too many objects. Use stringbuffer instead of string connections.
3. Use as few global variables as possible, such as static (create in heap, slow) and local variables (create in stack, fast ).
4. 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. That is
ClassTest {
PrivateObject OBJ;//Private object OBJ = NULL is not required;
}
5. In Java + Oracle application system development, the SQL language embedded in Java should be written in uppercase as much as possible to reduce the parsing burden on the Oracle parser.
6. During Java programming, database connections and I/O Stream operations are performed. after use, the database is closed in time to release resources. Because the operations on these large objects will cause a large overhead of the system.
7. Excessive object creation will consume a large amount of memory in the system. In severe cases, memory leakage will occur. Therefore, it is important to ensure timely recovery of expired objects.
Jvm gc is not very intelligent. Therefore, we recommend that you manually set it to null after the object is used.
8. When using the synchronization mechanism, try to use the synchronization method instead.CodeFast synchronization.
9. reduce repeated calculation of variables as much as possible.
For example:
For(IntI = 0; I <list. Size (); I ++)
Should be replaced:
For(IntI = 0,IntLen = List. Size (); I <Len; I ++)
10. Create a variable or object only when necessary.
For example:
String STR = "hello ";
If(I = 1 ){
List. Add (STR );
}
Should be replaced
If(I = 1 ){
String STR = "hello ";
List. Add (STR );
}
11. 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 for control.ProgramProcess.
12. Do not use try/catch statements in a loop. If necessary, place them in the outermost layer.
13. If a class implements cloneable, call the clone () method to create an instance of the class. This is because all constructors in the constructor chain are automatically called when new is created.
14. multiplication and division
Consider the following code:
For(Val = 0; Val <100000; Val + = 5 ){
Alterx = Val * 8; myresult = Val * 2;
}
Replacing multiplication with shift operations can greatly improve performance. The modified code is as follows:
For(Val = 0; Val <100000; Val + = 5 ){
Alterx = Val <3; myresult = Val <1;
}
The modified code does not multiply by 8. Instead, it uses the equivalent three-bit left shift operation, and one-bit left shift operation is equivalent to multiplying 2. Correspondingly, the one-bit operation on the right is equivalent to dividing by 2. It is worth mentioning that, although the shift operation is fast, it may make the code more difficult to understand, so it is best to add some comments.
15. Use hashmap and arraylist whenever possible. Unless necessary, hashtable and vector are not recommended. The latter causes performance overhead due to synchronization mechanism.
16. If you know the length, try to use a fixed-length array.
17. When copying a large amount of data, use the system. arraycopy () command.