1. Garbage CollectionAlgorithmCore Idea
The Java language sets up a garbage collection mechanism to track the objects in use and discover and recycle objects that are no longer used (referenced. This mechanism can effectively prevent two risks that may occur in dynamic memory allocation: Memory depletion caused by excessive memory spam and illegal Memory Reference caused by improper memory release.
The core idea of the garbage collection algorithm is to identify the available memory space of the virtual machine, that is, the objects in the heap space. If the object is being referenced, it is called a surviving object. Otherwise, if an object is no longer referenced, It is a spam object and the occupied space can be reclaimed for redistribution. The selection of the garbage collection algorithm and the reasonable adjustment of the garbage collection system parameters directly affect the system performance. Therefore, developers need to have a deeper understanding.
2. Conditions for triggering the master GC (Garbage Collector)
The frequency of jvm gc is very high, but this GC takes a very short time, so it has little impact on the system. It is worth noting that the trigger condition of the main GC is obvious to the system. In general, there are two conditions that will trigger the main GC:
① When the applicationProgramWhen idle, GC is called when no application thread is running. GC is performed in the thread with the lowest priority, so when the application is busy, the GC thread will not be called, except for the following conditions.
② When the Java heap memory is insufficient, GC will be called. When the application thread is running and a new object is created during running, if the memory space is insufficient, JVM will forcibly call the GC thread to recycle the memory for new allocation. If the GC does not meet the memory allocation requirements after one time, the JVM will perform two further GC attempts. If the GC fails to meet the requirements, the JVM reports an error of "out of memory" and the Java application stops.
The JVM determines whether to perform the primary GC based on the system environment, and the system environment is constantly changing. Therefore, the operation of the primary GC is uncertain and cannot be predicted when it will inevitably emerge, however, it can be determined that the main GC is repeated for a long-running application.
3. Measures to Reduce GC overhead
According to the above GC mechanism, program running will directly affect the changes in the system environment, thus affecting GC triggering. If the GC features are not designed and encoded, a series of negative effects such as memory resident will occur. To avoid these impacts, the basic principle is to minimize garbage and reduce GC overhead. Specific measures include the following:
(1) do not explicitly call system. GC ()
The JVM is recommended to perform primary GC for this function. Although it is only recommended, it will trigger the primary GC in many cases to increase the frequency of primary GC, this increases the number of intermittent pauses.
(2) minimize the use of temporary objects
After a function call is called, the temporary object will become garbage. Using less temporary variables is equivalent to reducing the generation of garbage, thus increasing the time for the second trigger condition, reduces the chance of main GC.
(3) it is best to explicitly set the object to null when it is not used.
Generally, null objects are treated as garbage. Therefore, explicitly setting unused objects to NULL is helpful for GC collectors to identify garbage and improve GC efficiency.
(4) Try to use stringbuffer instead of string to accumulate strings (For details, refer to another article on Blog.ArticleString and stringbuffer in Java)
Because string is a fixed-length String object, when adding a String object, it is not expanded in a string object, but a new String object is created, for example, if str5 = str1 + str2 + str3 + str4, multiple spam objects are generated during the execution of this statement, because a new String object must be created during the "+" operation, however, these transition objects have no practical significance for the system and only increase more garbage. To avoid this situation, you can use stringbuffer to accumulate strings. Because stringbuffer is variable-length, it is expanded based on the original, without generating intermediate objects.
(5) You can use basic types such as int and long to remove integer and long objects.
The memory resources occupied by the basic type variables are much less than those occupied by the corresponding objects. If not necessary, it is best to use the basic variables.
(6) Use as few static object variables as possible
Static variables are global variables and will not be recycled by GC. They will continue to occupy the memory.
(7) time when the dispersed object was created or deleted
When a large number of new objects are created in a short period of time, especially large objects, a large amount of memory is suddenly required. In this case, the JVM can only perform primary GC, to recycle memory or integrate memory fragments to increase the frequency of the primary GC. The same applies to deleting objects in a centralized manner. It causes a large number of spam objects to suddenly appear, and the free space is inevitably reduced, which greatly increases the chance to force the main GC when the next object is created.