- (1) do not explicitly call system. GC ()
- (2) minimize the use of temporary objects
- (3) it is best to explicitly set the object to null when it is not used.
- (4) Try to use stringbuffer instead of string to accumulate the string (For details, refer to the string and stringbuffer in another blog article in Java)
- (5) You can use basic types such as int and long to remove integer and long objects.
- (6) Use as few static object variables as possible
- (7) time when the dispersed object was created or deleted
- (1) reference the counting collector
- (2) tracking collector
Java deep adventure (4) -- Java garbage collection mechanism and reference type
JVM concept-Java object size and reference type
1. Core Idea of the garbage collection Algorithm
Java language sets up a garbage collection mechanismTracking the objects in use and discovering and recycling objects that are no longer in use (reference). 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: memory space available for virtual machines, that isHeap SpaceIf the object is being referenced, it is called a surviving object. Otherwise, if the object is no longer referenced, It is a spam object and the occupied space can be reclaimed for redistribution.Garbage collection AlgorithmThe selection and reasonable adjustment of garbage collection system parameters directly affect the system performance. Therefore, developers need to have a deeper understanding.
Java garbage collection mechanism (PS: 2012-6-8)
The Java garbage collector is responsible for completing three tasks:A) allocate memory,B) ensure that the memory of the referenced object is not recycled incorrectly.AndC) reclaim the memory space of objects that are no longer referenced. Garbage collection is a complex and time-consuming operation. If the JVM spends too much time on garbage collection, the running performance of the application will inevitably be affected. In general, when the garbage collector is performing the recycle operation, the execution of the entire application isStop-the-world). This is because the Garbage Collector needsUpdate the actual memory address referenced by all objects in the Application(Update the actual memory address referenced by all objects in the application becauseMemory fragmentation, resulting in changes to the physical address of the object). Different hardware platforms support different garbage collection methods. For exampleMulti-CPU PlatformYou can useParallel garbage collection. WhileSingle CPU PlatformOnlySerial. Different applications may expect different garbage collection methods. The server application may want to spend less time on garbage collection during the entire running time of the application, the better (In this way, you only need to recycle the server at a fixed time, similar to the server maintenance of World of Warcraft every Tuesday.). For applications that interact with users, it is possible that the interval between application pauses caused by garbage collection is smaller, the better (This reduces long waiting time in human-computer interaction.). In this case, JVM provides a variety of garbage collection methods and corresponding performance tuning parameters. Applications can be customized as needed.
The most basic method of Java garbage collection mechanism isGeneration). The regions in the memory are divided into different generations, and objects are stored in the region of the corresponding generation according to their survival time. Generally, it is divided into three generations:Young, old, and permanent. Memory Allocation occurs in the young generation. When an object remains long enough, it will be copied to the older generation. Different garbage collection algorithms can be used for different generations. The starting point of generation Division is the statistical rule obtained after studying the object survival time in the application. In general, the survival time of most objects in an application is very short. For example, the survival time of local variables is only in the method execution process. Based on this, the garbage collection Algorithm for the young generation can be very targeted.
Memory areas of the young generation are further dividedEden space, from Region vor space, to region vor Space.Eden SpaceIt is the place where the memory is allocated and a continuous idle memory area. The above memory allocation speed is very fast, because you do not need to find available memory blocks.From region vor space and to region vor SpaceOne of the two survival zones is always blank. During garbage collection, Eden and one of the non-empty survival zonesFrom region vor SpaceObjects that are still alive are copied to the active zone of the current blank according to their survival time.To allocate vor SpaceOr older generation. After this copy, the previous non-empty survival zoneTo allocate vor SpaceContains the currently alive objects, while Eden and another alive ZoneFrom region vor SpaceThe content in is no longer needed. You just need to simply clear the two regions.During the next garbage collection, the roles in the two survival zones were exchanged.. Generally, the young generation has a small area and most objects are no longer alive. Therefore, it is more efficient to search for surviving objects.
For memory regions of older and permanent generations, different recycling algorithms are used, called Mark-sweep-compact )".MarkThe process is to find and mark the currently alive objects;ClearTraverse the entire memory area to find the area where recovery is needed.CompressionThe memory of the surviving object is moved to one end of the entire memory area, so that the other end is a continuous idle area for convenient memory allocation and replication.
JDK 5 provides four different garbage collection mechanisms. The most common method is serial collection, that is, using a single CPU to reclaim memory for young and old generations. During the recycle process, the application is temporarily suspended. The recycling method is the most basic generational recycling mentioned above. The serial recycling method is suitable for a Single-CPU desktop platform. If it is a multi-CPU platform, it is suitable for parallel recovery. In this way, multiple CPUs are used for parallel processing when the young generation is recycled, which can improve the recovery performance. The concurrent tag-clear collection method is suitable for scenarios with high response time requirements for applications, that is, the temporary suspension time of applications caused by garbage collection needs to be reduced. The advantage of this method is that it can mark the alive object and recycle garbage while the application is running, but it only needs to suspend the application for a short time.
Using the jconsole provided in JDK, you can easily view the memory usage of the current application. When JVM is started, add the parameter-verbose: GC to view the running result of the garbage collector.
2. Conditions for triggering the master GC (Garbage Collector)
JVMSecondary GCHowever, this GC takes a very short time, which has little impact on the system. More noteworthy is thatMain GCBecause it has obvious impact on the system. In general, there are two conditions that will trigger the main GC:
(1) When the application is idle, that is, when no application thread is running, GC will be called.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.
(2)GC is called when the Java heap memory is insufficient.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 problem persists, the JVM reports an "out of memory" error and the Java application stops.
Why do I have to execute two GC operations if the memory is insufficient after one GC operation? (PS: 2012-6-8)
According to the previous blog: JVM concept, the size and reference type of Java objects, as mentioned in this article:"Soft references are generally used as cache. The difference between soft reference and strong reference is that when soft reference is garbage collection, the virtual opportunity determines whether to recycle soft reference based on the remaining memory of the current system. If the remaining memory is insufficient, the space referenced by the soft reference will be reclaimed by the virtual opportunity. If the remaining memory is relatively rich, the space will not be recycled. In other words, when a Virtual Machine occurs in outofmemory, there must be no soft reference."I think the JVM determines whether to report the" out of memory "error only after two GC operations are performed.Reclaim soft referencesIf the memory is enough after soft reference is recycled, the memory space will be allocated. If soft reference is recycled or the memory is not enough, the "out of memory" error will be reported. This is why the article emphasizes:"When a Virtual Machine occurs in outofmemory, there must be no soft reference ."
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.It is particularly worth noting that the call system shown in the code. GC () does not necessarily support GC. We can verify this by using the finalize () method, that is, actively calling system. GC () does not always call the finalize () method every time. For details, refer to the Code instance in the difference of the blog final finally finalize. The finalize () method first calls the finalize () method before the object is recycled.
(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 the string (For details, refer to the string and stringbuffer in another blog article 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.Under what circumstances should integer be used?This depends on the specific situation, such as type conversion. If the integer type is required to be easily converted to the string typeInteger I = 30, You can directly view the sameString STR = I. tostring ();But if int I = 30 is used, there is no I. tostring () method.
(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.
4. Garbage collection algorithm (1) reference counting collector
Reference count is an early policy for garbage collection. In this method, each object in the heap has a reference count. When an object is created and a reference pointing to the object is assigned to a variable, the reference count of this object is set to 1. For example, to create a new object A = new A (), and then a is assigned to another variable B, that is, B = A; then the reference count of object A is + 1. When any other variable is assigned a reference to this object, the count is incremented by 1. When the reference of an object exceeds the lifetime or a new value is set, the reference count of the object is reduced by 1. For example, if B is set to C, the reference count of A is-1. Any object with 0 reference count can be used as garbage collection. When an object is garbage collected, the number of objects it references is reduced by 1. In this method, the garbage collection action of other objects may occur after an object is collected by garbage collection. For example, a A = new A (); B = A; when B is reclaimed, the reference count of A is changed to 0, which causes a to be recycled.
Benefits:The reference counting collector can be executed quickly and intertwined in the running of the program. This reminder is advantageous for real-time environments where the program cannot be interrupted for a long time.
Disadvantages of methods:The reference count cannot detect loops (that is, two or more objects reference each other ). For example, the parent object has a child object reference, and the child object references the parent object in turn. In this way, object users cannot count to 0 even if they cannot be reached by the root object of the execution program. Another disadvantage is that each increase or decrease in the reference count brings additional overhead.
(2) tracking collector
Spam is usually detected by creatingRoot objectAnd checkAccessibility. If there is a reference path between the root object that can be accessed by the program being executed and an object, this object is accessible. For programs, the root object is always accessible. Starting from these root objects, any objects that can be touched are considered as "active" objects. Objects that cannot be touched are considered as spam because they do not affect the future execution of the program.
The tracking collector is used to trace the object reference graph starting from the root node. Objects encountered during trackingMark with a hand. In general, eitherObject itselfSet the tag, or use an independentBitmapTo set the tag. When tracing ends, unlabeled objects cannot be reached and can be collected.
A basic tracing algorithm is called "mark and clear ". This name indicates two stages of spam. In the tag phase, the garbage collector traverses the reference tree to mark every object encountered. During the cleanup phase, unlabeled objects are released, and the memory obtained after the objects are released is returned to the executing program. In a Java virtual machine, the cleanup step must include the end of the object.