Recently I have been learning the knowledge of project optimization. Although JVM optimization, database optimization and project architecture optimization can improve the performance of the project to a certain extent, but if the code itself has performance problems, how to optimize in other places will not achieve the optimal effect

Code optimization

  1. Use local variables whenever possible

    • Passing parameters when calling a method and temporary variables created during the call are fast on the stack, while other static variables, instance variables, large objects, etc., are slow on the heap.
    • No extra GC is needed: objects in the stack end with the method running, and the content is gone
  2. Minimize double counting of heap variables

    • This is the time to make it clear that a call to a method, even if there is only one statement in the method, is costly. So for example, do the following
    for(int i = 0; i < list.size(); i++){ ....... } I used to write like thisCopy the code

    You are advised to replace it with:

    int length = list.size();
    for (int i = 0; i < length; i++){
        ............
    }
    Copy the code

    If list.size() is large, it saves a lot of energy

  3. Try to use the lazy load strategy, which is when to use when to load

    String str = "my";
    if(1 == i){ list.add(str); } // It is recommended to replaceif (1 == i){
        String str = "my";
        list.add(str);
    }
    Copy the code
  4. Exceptions should not be used to control program flow

    The constructor of the Throwable interface calls a locally synchronized method called fillInStackTrace(). The fillInStackTrace() method checks the stack and collects call trace information. Whenever an exception is thrown, The Java virtual machine must call the stack because a new object is created during processing, and exceptions can only be used for error handling and should not be used to control program flow

  5. Do not declare arrays as public static final(have you ever written this)

    Making the array public is a security loophole, which means that the array can be changed by an external class.

  6. Don’t create objects that you don’t use, and don’t import classes that you don’t use

    It doesn’t make sense to do this without creating an import. When “The value of The local variable is not used” or “The import java.util is never used” appear in your code, delete these useless contents

  7. Avoid using reflection when your program is running

Reflection is a very powerful function provided by Java to users. Powerful functions often represent low efficiency. Do not use the reflection mechanism, especially the Invoke Method, during program running. Of course, if it must be used, an object will be instantiated by reflection and put into memory as soon as the project starts.Copy the code
  1. Use database connection pools and thread pools

    Both pools are designed to reuse objects, the former to avoid frequent opening and closing of connections, and the latter to avoid frequent thread creation and destruction

  2. Specify the length whenever possible when the container is initialized

    Such as new ArrayList < > (10); new HashMap<>(32); Avoid performance consumption caused by capacity expansion when the container length is insufficient

  3. ArrayList is fast to randomly traverse, LinkedList is fast to add and delete

ArrayList index traversal is fast, LinkedList add and delete don't need to move just change the pointCopy the code
  1. Use Entry to traverse the Map
``` Map<String,String> map = new HashMap<>(); for (Map.Entry<String,String> entry : map.entrySet()){ String key = entry.getKey(); String value = entry.getValue(); } Map<String,String> Map = new HashMap<>(); for (String key : map.keySet()){ String value = map.get(key); } ` ` `Copy the code
  1. Do not call system.gc () manually;
If you call this manually, you might be clouding the system's judgment, trusting that the JVM will know better when to GC.Copy the code
  1. Use regular expressions sparingly for String

    Replace () is not supported for regular replaceAll(). Replace () is recommended for string substitutions only.

  2. Log output attention level

// The current log level is Error logger. info(" Save error!" + user); Try {xxx.close(); yyy.close(); } catch (Exception e){ ..... } // Try {xxx.close(); } catch (Exception e){ .... } try{ yyy.close(); } catch (Exception e){ } ```Copy the code