01. Avoid arbitrary use of static variables as far as possible
When an object is defined as a reference to a STATAIC variable, the GC usually does not reclaim the memory that the object occupies, such as:
1:public class a{
2: static b = b = new B ();
3: }
At this point, the life cycle of static variable B is synchronized with Class A, and if Class A does not unload, then the B object will reside in memory until the program terminates.
02. Try to avoid creating Java objects too often
Try to avoid the methods that are frequently invoked, the new object in the loop, because the system not only takes time to create objects, but also takes time to garbage collection and processing of these objects, in the range we can control to maximize the reuse of objects, preferably with a basic data type or array to replace the object.
03. Use final modifier as far as possible
A class with the final modifier is not derived. If 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%.
04. 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.
05. Use StringBuilder and StringBuffer for string connection as far as possible
However, it is also best to specify the capacity first. For example, the StringBuffer constructor creates a character array of the default size (usually 16). In use, if this size is exceeded, the memory is reassigned, a larger array is created, the original array is copied, and the old array is discarded. In most cases, you can specify the size when creating stringbuffer, thus avoiding automatic growth when there is not enough capacity to improve performance.
06. Single thread should try to use HashMap, ArrayList 07. Create HashMap as reasonable as possible
When you want to create a larger HashMap, make full use of another constructor public HashMap (int initialcapacity, float loadfactor) to avoid HashMap multiple hash refactoring, Expansion is a very performance-intensive thing, in the default initialcapacity only 16, and Loadfactor is 0.75, how much capacity, you'd better accurately estimate the best size you need, the same hashtable,vectors is the same truth.
08. To minimize the duplication of the calculation of variables
Such as
1:for (int i=0; i<list.size (); i++)
should read
1:for (int i = 0, Len = list.size (); i < Len; i++)
Or
1:for (int i = List.size ()-1; I >= 0; i)
And in the loop should avoid the use of complex expressions, in the loop, the loop condition will be repeatedly computed, if not the use of complex expressions, and the loop condition value unchanged, the program will run faster.
09. Try to avoid using two-dimensional arrays
Two-dimensional data occupies much more memory space than a one-dimensional array, about 10 times times more.
10.ArrayList & LinkedList
One is a linear table, one is linked list, understand the data structure should know what the difference, pay attention to the right remedy.
11. Try to use System.arraycopy () instead of iterating through the array
System.arraycopy () is much faster than replicating an array by looping.
12. Shift Operation
For "/" and "*" operations, it is quicker and more efficient to use the shift operation.
13. Release the references to the unwanted objects as early as possible
Most of the time, the object referenced by the method local reference variable becomes garbage as the method ends, so most of the time the program does not need to explicitly set the local, reference variable to NULL. When a local variable is not needed, it does not need to be clearly set to NULL, because when a method finishes executing, the references are automatically cleaned up. But for the following scenario:
1:public void Test () {
2: Object obj = new Object ();
3: ...
4: obj=null;
5://execute time-consuming, memory-intensive operation, or call time-consuming, memory-consuming methods
6: ...
7: }
At this point, obj can be assigned to NULL, and the reference to object objects can be released as soon as possible. Obj=null just tell the JVM that this object has become rubbish and when it is recycled, it is not sure.
14. Try to avoid using split
Unless it is necessary, you should avoid using split,split because of the support of regular expressions, so inefficient, if the frequent dozens of, millions of of the call will consume a lot of resources, if you do need to frequently call split, You can consider using Apache Stringutils.split (String,char), which can cache results with frequent split.
15. Cache frequently used objects as much as possible
to cache frequently used objects as much as possible using arrays, or hashmap containers, but this may cause the system to consume too much cache and degrade performance, recommending the use of some third-party open source tools, such as Ehcache , Oscache cache, they basically implement the Fifo/flu cache algorithm.