1. Try to avoid using static variables arbitrarily
When an object is defined as a variable reference to static, the GC typically does not reclaim the memory used by the object
public class A {
Static b b = new B (); The life cycle of static variable B is synchronized with Class A, so long as Class A is not unloaded, the B object will reside in memory until the program terminates
}
2. Handling of packaging types and basic types of use of the site
Although the packing type and the basic type can be converted to each other during use, the memory area produced by both of them is completely different, and the basic type data is generated and processed in the stack;
The wrapper type is an object that produces an instance in the heap.
3. Use synchronized carefully
The implementation of synchronization is a significant cost to the system, and may even cause deadlocks, so try to avoid unnecessary synchronization control.
4. Single thread try to use HashMap, ArrayList
HashTable, vectors, etc. use the synchronization mechanism to reduce the performance.
5. Minimize the repeated calculation of variables
for (int i = 0; i < list.size (); i++)//loop, cyclic conditions are calculated repeatedly
Should be changed to for (int i = 0, Len = list.size (); i < Len; i++)
6. Try to avoid unnecessary creation
A = new A ();
if (i = = 1) { list.add (a); }
Should be changed to: if (i = = 1) { A A = new A (); List.add (a); }
7. Try to determine the capacity of the StringBuilder
The StringBuilder constructor creates an array of characters with a default size (usually 16), which, if exceeded, allocates memory, creates a larger array, copies the original array, and discards the old array. In most cases, you can specify the size when creating the StringBuilder, which avoids auto-growth when the capacity is insufficient to improve performance.
8. Release references to useless 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 have to set the local reference variable to NULL, but if the object is large and takes up more memory, it should be manually empty when not in use.
Java Program Performance optimization