In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the Java garbage collection algorithm, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
The Core idea of 1.Java garbage Collection algorithm
The Java language establishes a garbage collection mechanism to track objects in use and to discover and recycle objects that are no longer in use (references). Java garbage collection algorithm can effectively prevent two dangers that may occur in dynamic memory allocation: memory exhaustion caused by excessive memory garbage and illegal memory references caused by inappropriate memory release.
The core idea of the garbage collection algorithm is to identify the available memory space of the virtual machine, that is, the object in the heap space, and if the object is being referenced, it is called a living object, on the contrary, if the object is no longer referenced, it is a garbage object. the space it occupies can be recycled for redistribution. The choice of garbage collection algorithm and the reasonable adjustment of garbage collection system parameters directly affect the system performance, so developers need to do more in-depth understanding.
two。 Conditions for triggering the main GC (Garbage Collector)
The frequency of sub-GC in JVM is very high, but because this kind of GC takes up very short time, it has little impact on the system. Of more concern is the trigger condition of the main GC, because it has a significant impact on the system. In general, there are two conditions that trigger the primary GC:
① GC is called when the application is idle, that is, when no application thread is running. Because GC occurs in a thread of priority * *, the GC thread will not be called when the application is busy, except for the following conditions.
GC is called when the ② Java heap is out of memory. When the application thread is running and a new object is created during the run, if there is not enough memory space, JVM will forcibly call the GC thread to reclaim memory for new allocation. If GC still fails to meet the memory allocation requirements after one time, JVM will make two further attempts with GC. If it still fails to meet the requirements, JVM will report a "out of memory" error and the Java application will stop.
Because whether or not to carry out the main GC is decided by the JVM according to the system environment, and the system environment is constantly changing, the operation of the master GC is uncertain, and it is impossible to predict when it will inevitably appear, but it is certain that for a long-running application, the main GC is repeated.
3. Measures to reduce GC overhead
According to the above GC mechanism, the operation of the program will directly affect the change of the system environment, thus affecting the trigger of GC. If it is not designed and coded according to the characteristics of GC, there will be a series of negative effects such as memory retention. To avoid these effects, the basic principle is to reduce as much garbage and overhead as possible in the GC process. Specific measures include the following aspects:
(1) do not explicitly call System.gc ()
This function advises JVM to carry out the main GC, although it is only recommended rather than certain, but in many cases it will trigger the main GC, thus increasing the frequency of the main GC, that is, increasing the number of intermittent pauses.
(2) minimize the use of temporary objects
After jumping out of the function call, the temporary object will become garbage, and using fewer temporary variables is equivalent to reducing the generation of garbage, thus prolonging the occurrence time of the second trigger condition mentioned above and reducing the chance of the main GC.
(3) the object is explicitly set to Null when it is not in use
Generally speaking, objects that are Null will be treated as garbage, so explicitly setting unused objects to Null is helpful for the GC collector to determine garbage, thus improving the efficiency of GC.
(4) try to use StringBuffer instead of String to accumulate strings (see String and StringBuffer in JAVA, another article in blog)
Because String is a fixed-length string object, when accumulating String objects, instead of expanding in a String object, but recreating new String objects, such as Str5=Str1+Str2+Str3+Str4, many junk objects will be generated during the execution of this statement, because new String objects must be created during the secondary "+" operation, but these transition objects have no practical significance to the system, and will only add more garbage. To avoid this situation, you can use StringBuffer to accumulate strings instead, because StringBuffer is variable-length, it expands on the original basis, and does not produce intermediate objects.
(5) if you can use basic types such as Int,Long, you don't need Integer,Long objects.
Basic type variables take up much less memory than the corresponding objects, and use basic variables if not necessary.
(6) use static object variables as little as possible
Static variables are global variables and will not be recycled by GC. They will always take up memory.
(7) time when scattered objects are created or deleted
Focusing on creating a large number of new objects, especially large objects in a short period of time, will lead to a sudden need for a large amount of memory. When faced with this situation, JVM can only carry out the main GC to reclaim memory or consolidate memory fragments, thus increasing the frequency of the primary GC. The same is true for centrally deleting objects. It makes a large number of junk objects appear suddenly, and the free space is bound to be reduced, thus greatly increasing the chances of forcing the main GC the next time a new object is created. It can be said that the GC mechanism based on Java garbage collection algorithm effectively reduces the errors of Java in memory management.
After reading the above, have you mastered how to understand the Java garbage collection algorithm? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.