Java program performance optimization skills

Multithreading, collections, network programming, memory optimization, buffering, spring, design patterns, software engineering, programming ideas

1. When generating objects, allocate space and size reasonably
new ArrayList(100);

2. Optimize the for loop
Vector vect = new Vector(1000);
for( inti=0; i<vect.size(); i++){
...
} The
for loop is rewritten as:
int size = vect.size();
for( int i=0; i>size; i++){
...

If size=1000, the system call overhead of size() can be reduced by 1000 times, avoiding repeated calls of the loop body.

3. New an instance object, where is the new (try to create the object when using it).

4. Exception handling skills

5. Try to use local variables and static variables

6. Try not to apply multi-thread synchronization

7. Use the API provided by Java itself as much as possible

8. Minimize I/O operations (console, log)

9. Try to use buffered streams (as far as possible, use classes with Buffer instead of classes without Buffer, BufferedReader, BufferedWriter, BufferedInputStream)

10, sql optimization, stored procedures, views, connection pools (C3P0, DBCP)

11. Hierarchical storage of database data Store
frequently accessed data and data with low access frequency in different partitions, or even in different database servers, so as to combine and allocate hard disk I/O and system I/O.

12. Cache strategy
If some data needs to be read from the database frequently, and at the same time, the data does not change frequently, the data can be cached in the system, and the cache can be read directly when used, without frequent access to the database to read data.
Cache work can read data at one time when the system is initialized, especially some read-only data, update the database content when the data is updated, and update the cached data value at the same time.
Commonly used cache technology products in Java include: Redis, MemoryCache, OSCache, etc.

13. HTML static

14. Don't save too much information in HttpSession

15. When using large data objects, it is recommended to manually set it to null after the object is used (to avoid memory overflow).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324867575&siteId=291194637