Java program performance optimization skills
Multithreading, collection, network programming, memory optimization, buffering, spring, design pattern, software engineering, programming ideas
1. reasonably allocate space and size when generating objects
New ArrayList (100 );
2. Optimize the for Loop
Vector vect = new Vector (1000 );
For (inti = 0; I <vect. size (); I ++ ){
...
}
The for loop part is rewritten:
Int size = vect. size ();
For (int I = 0; I> size; I ++ ){
...
}
If the size is 1000, the system call overhead of the size () can be reduced by 1000 times, avoiding the loop weight of the system.
3. new instance object, where new is located (try to create this object again when used ).
4. Exception Handling Skills
5. Use local variables and static variables whenever possible
6. multithreading synchronization is not applicable as far as possible
7. Use APIs provided by Java as much as possible
8. Minimize I/O operations (console and logs)
9. Use cache streams whenever possible (use classes with Buffer instead of classes without Buffer, BufferedReader, BufferedWriter, and BufferedInputStream)
10. SQL optimization, stored procedures, views, and connection pools (C3P0 and DBCP)
11. Hierarchical Storage of Database Data
Store frequently accessed data and infrequently accessed data in different partitions, or even in different database servers, so that they can be integrated into and allocated hard disk I/O and system I/O.
12. Cache Policy
If some data needs to be read from the database frequently and does not change frequently, the data can be cached in the system and directly read during use, instead of frequently accessing the database to read data.
Cache can read data at one time during system initialization, especially some read-only data. when the data is updated, the database content is updated and the cached data value is updated.
Common Java cache technologies include Redis, MemoryCache, and OSCache.
13. static html
14. You do not need to save too much information in HttpSession.
15. When using a big data object, we recommend that you manually set it to null (to avoid memory overflow) after the object is used ).