1. Not only does the system take time to create objects, but it takes time to recycle and process these objects, so you should try to avoid too many frequently created objects that can be used to replace objects with basic data types or arrays;
2. When an object is defined as being referenced by a static variable, the memory occupied by the object is not recycled, so avoid using static variables as much as possible;
3. The parameters passed when the method is called and the temporary variables created in the call are saved in the stack and are faster. Other variables, such as static variables, instance variables, and so on, are created in the heap and are slower. Therefore, local variables should be used as much as possible;
4. Typically, the StringBuffer constructor creates a character array of the default size (16), which, when used, will reallocate memory, create a larger array, copy the original array, and discard the old array. If you specify the size when creating the StringBuffer, this avoids the situation of auto-growth when the capacity is not enough, so the capacity of stringbuffer should be determined as far as possible;
5. Two-D data occupies much more memory space than one-dimensional array, so we should avoid using two-dimensional array;
6. When creating an exception, you need to collect a stack trace that describes where the exception was created. Building these stack traces requires a snapshot of the runtime stack, which is a very expensive part. When you need to create a Exception, the stack and the stack operation are temporarily stopped. That is, creating an exception requires a lot of overhead. So be cautious with exceptions.
Recent gains in learning Java