003 Garbage collector and memory allocation policy

Source: Internet
Author: User

1. Overview program counters, virtual machine stacks, the local method stack is thread-private, memory allocation and recycling are deterministic, do not need to consider the problem of garbage collection, the end of the method or the end of the thread, memory is naturally recycled Java heap and method area of memory allocation and recycling are dynamic, The garbage collector is concerned about this part of the memory
2. The object garbage collector that is processed by the garbage collector needs to determine which objects are "alive" and which have "died" (objects that cannot be used by any means) ① The reference counting algorithm adds a reference counter to the object, and the counter value is incremented by 1 whenever there is a reference to it; The counter value is reduced by 1, and any object with counter 0 at any time is impossible to be used again.
There is no reference counting algorithm for mainstream Java virtual machines because it is difficult to solve the problem of circular referencing between objects
The basic idea of ② algorithm is to search through a series of objects called "GC Roots" as the starting point, starting from these nodes, searching through the path called the reference chain (Reference Chain), when an object to the GC Roots no reference chain is connected (in the words of graph theory, that is, from the GC roots to the object unreachable), it proves that this object is not available. As shown, objects 5, 6, and object 7 are associated with each other, but they are not accessible to GC roots, so they will be judged as recyclable objects.


In Java, the objects that can be used as GC roots are:
    • The object referenced in the virtual machine stack (the local variable table in the stack frame).
    • The object referenced by the class static property in the method area.
    • The object referenced by the constant in the method area.
    • The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.

After ③ references JDK1.2, the references are divided into strong references (strong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Reference)
    • A strong reference is a common reference in program code, such as "Object obj = new Object ()", as long as a strong reference exists, and the garbage collector never reclaims the referenced object.
    • Soft references are used to describe objects that are useful but not necessary. For objects associated with soft references, these objects are then listed in the collection scope for a second collection before the system is about to occur with a memory overflow exception. If this collection does not have enough memory, a memory overflow exception will be thrown. After JDK 1.2, the SoftReference class was provided to implement soft references.
    • A weak reference is also used to describe a non-required object, but its strength is weaker than a soft reference, and the object associated with the weak reference only survives until the next garbage collection occurs. When the garbage collector is working, the objects associated with a weak reference are reclaimed regardless of whether the current memory is sufficient. After JDK 1.2, the WeakReference class was provided to implement weak references.
    • A virtual reference, also known as a phantom reference or phantom Reference, is the weakest reference relationship. Whether an object has a virtual reference exists, does not affect its lifetime at all, and cannot obtain an object instance through a virtual reference. The only purpose of setting a virtual reference association for an object is to be able to receive a system notification when the object is reclaimed by the collector. After JDK 1.2, the Phantomreference class is provided to implement the virtual reference.

④ even in the accessibility analysis algorithm unreachable objects, is not "dead", when they are temporarily in the "probation" stage, to truly declare an object to die, at least two times to go through the marking process: If the object is found to be in the accessibility analysis of the GC roots linked to the reference chain, Then it will be marked for the first time and filtered to see if it is necessary for this object to execute the Finalize () method. When the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "no need to execute". If the object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue called F-queue and then executed by a low-priority finalizer thread that is automatically created by the virtual machine at a later time. The so-called "execution" here means that the virtual opportunity triggers this method, but does not promise to wait for it to end, because if an object executes slowly in the Finalize () method, or if a dead loop (more extreme) occurs, Will most likely cause other objects in the F-queue queue to be permanently waiting, even causing the entire memory-recycling system to crash. The Finalize () method is the last chance for an object to escape the fate of death, and later the GC will make a second small-scale mark on the object in F-queue, if the object is to successfully save itself in Finalize ()-just re-associate with any object on the reference chain. For example, assigning yourself (the This keyword) to a class variable or to a member variable of an object, it will be removed from the collection that is "about to be recycled" at the second mark, and if the object has not escaped at this time, it is basically recycled. The Finalize () method of any object is called only once by the system, and if the object faces the next collection, its Finalize () method will not be executed again, so the second code's self-help action fails.

⑤ recovery method Area in the heap, especially in the Cenozoic, the general application of a garbage collection generally can reclaim 70%~95% space, and the permanent generation of garbage collection efficiency is much lower than this. The garbage collection of the permanent generation mainly recycles two parts: obsolete constants and useless classes. Reclaiming obsolete constants is very similar to reclaiming objects in the Java heap. The recovery constant (literal) is relatively simple, as long as there is no reference to the literal, and if necessary, recycling. The criteria for judging "useless classes" are more demanding and need to be met:
    • All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap.
    • The ClassLoader that loaded the class have been recycled.
    • The corresponding Java.lang.Class object of this class is not referenced anywhere and cannot be used to access the class's methods at any place.
A useless class that satisfies the above three conditions can be recycled, unlike an object, which is necessarily recycled without being used. Whether the class is recycled, the hotspot virtual machine provides control of the-XNOCLASSGC parameter, and can also use-verbose:class and-xx:+traceclassloading,-xx:+ Traceclassunloading view class load and unload information, where-verbose:class and-xx:+traceclassloading can be used in the product version of the virtual machine,-xx:+ The traceclassunloading parameter requires virtual machine support for version fastdebug. 3. Garbage collection algorithm ① tag-purge algorithm algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the completion of the mark (accessibility analysis), the Unified collection of all tagged objects
There are two main deficiencies: one is the efficiency problem, the efficiency of marking and clearing two processes is not high, and the other is the space problem, after the mark is cleared, there will be a lot of discontinuous memory fragmentation, too much space fragmentation may cause later when the program is running to allocate a larger object, Unable to find enough contiguous memory and had to trigger another garbage collection action ahead of time.

The ② replication algorithm divides available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once.
This makes every time the entire half of the memory collection, memory allocation will not consider the complexity of memory fragmentation, as long as the mobile heap top pointer, in order to allocate memory, easy to implement, efficient operation.
Today's commercial virtual machines use this collection algorithm to reclaim the new generation. The new generation of 98% is "dying", so there is no need to divide the memory space according to the 1:1 ratio, but the memory is divided into a larger Eden space and two smaller survivor space, each using Eden and one of the survivor. When recycled, the objects that are still alive in Eden and survivor are copied one at a time into another survivor space, finally clearing up Eden and the survivor space just used. The default Eden and survivor size ratio of the hotspot virtual machine is 8:1, that is, each new generation of available memory space for the entire Cenozoic capacity of 90% (80%+10%), only 10% of the memory will be "wasted." Of course, 98% of the objects can be recycled only in the general scenario of the data, we have no way to ensure that only a few more than 10% per collection of objects to survive, when the survivor space is not enough, you need to rely on other memory (this refers to the old age) for the allocation of security (Handle Promotion).
If another piece of survivor space does not have enough space to hold the last surviving objects collected by the new generation, these objects will enter the old age directly through the allocation of security mechanisms.

③ tag-Collation algorithm the replication collection algorithm will perform more replication operations when the object survival rate is high, and the efficiency becomes lower. More crucially, if you do not want to waste 50% of space, you need to have additional space to allocate security, in order to deal with all the objects in the memory used in 100% survival extreme situation, so in the old age generally can not directly select this algorithm.
The tagging process is still the same as the tag-purge algorithm, and then all surviving objects are moved to one end, and then the memory outside the end boundary is cleaned up directly.

The ④ generational collection algorithm divides memory into chunks based on the lifetime of the object. The Java heap is generally divided into the new generation and the old age, so that according to the characteristics of each era to adopt the most appropriate collection algorithm. In the Cenozoic, every garbage collection is found to have a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects of the replication cost can be completed collection. In the old age, because the object has a high survival rate and no extra space to guarantee it, it must be recycled using the "mark-clean" or "mark-sweep" algorithm.


4. Hotspot algorithm implementation ① enumeration root node from the accessibility analysis from the GC roots node to find a reference chain as an example, can be used as GC roots node mainly in the global reference (such as constant or class static property) and the execution context (such as the local variable table in the stack frame), Many applications now have just hundreds of trillion of methods, and if you want to check the references, it will take a lot of time.
In addition, the sensitivity of the accessibility analysis to the execution time is also reflected in the GC pauses, as this analysis must be done in a snapshot that ensures consistency--"consistency" means that throughout the analysis the entire execution system looks like it is frozen at a certain point in time. It is not possible to show that the object reference relationship is still changing in the analysis process, and the accuracy of the analysis results cannot be guaranteed if the point is not satisfied. This is one of the important reasons why the GC must pause all Java execution threads (which Sun calls the "Stop the World"), even when enumerating the root nodes in a CMS collector called (almost) without a pause.
Virtual machines should have a way to know directly where the object references are stored, and in the implementation of the hotspot, a set of data structures called Oopmap are used to achieve this purpose. With the help of Oopmap, hotspot can quickly and accurately complete GC roots enumeration

The ② security point records this information in "specific locations", which are known as Security points (SafePoint), where the program does not stop at all places to start the GC and can only be paused when the security point is reached. The selection of SafePoint is neither too small to allow the GC to wait too long, nor too often to excessively increase the load at run time.
The selection of the security point is basically based on whether the program has a feature that allows the program to execute for a long time, and the most obvious feature of "long Execution" is the instruction sequence multiplexing, such as method invocation, loop jump, exception jump, etc., so instructions with these functions will produce safepoint
Let all the threads go to the nearest security point: Preemptive interrupt (almost no virtual machine using this method) and active interrupt preemptive interrupt does not require the execution code of the thread to actively cooperate, when the GC occurs, all the threads are first interrupted, if the thread break is found not on the security point, then restore the threads, let it "Run" to the point of safety.
The idea of active interruption is that when a GC needs to interrupt a thread, it does not directly operate on the thread, simply sets a flag, and the individual threads actively polling for the flag when the interrupt flag is true. The polling flag is coincident with the security point, plus the place where the object needs to be allocated memory.

The ③ security zone thread is in a sleep state or blocked state, when the thread is unable to respond to the JVM's interrupt request, "go" to a safe place to suspend the suspension, and the JVM is obviously unlikely to wait for the thread to be allocated CPU time again. In this case, the security zone (safe region) is required to resolve.
A security zone is a code fragment in which the reference relationship does not change. It is safe to start a GC anywhere in the region. We can also think of safe region as an expanded safepoint.
When the thread executes the code in safe region, it first identifies itself as having entered safe region, so that when the JVM launches the GC during that time, it does not have to identify itself as a safe region state. When the thread leaves the safe region, it checks to see if the system has completed the root node enumeration (or the entire GC process), and if it does, it continues, otherwise it must wait until it receives a signal that it can safely leave the safe region.

5. Garbage collector that the garbage collector virtual machine contains
The line is connected to a single-threaded collector that can be used with the ①serial collector: not just a CPU or a thread to complete the garbage collector, but when it comes to garbage collection, all other worker threads are the default Cenozoic collectors running in client mode for the virtual machine. The advantage is that it is simple and efficient (single-line turndown with other collectors), and for a single CPU environment, the serial collector is naturally able to achieve the highest single-thread collection efficiency due to the lack of thread interaction overhead. The serial collector is a good choice for virtual machines running in client mode.

The ②parnew collector parnew collector is actually a multithreaded version of the serial collector, and in addition to using multiple threads for garbage collection, the rest of the behavior includes all the control parameters available to the serial collector (for example:-xx:survivorratio,-xx: Pretenuresizethreshold,-xx:handlepromotionfailure, etc.), collection algorithms, Stop the world, object assignment rules, and recycling policies are all identical to the serial collector.

is the preferred Cenozoic collector for many virtual machines running in server mode, one of which is performance-independent but important because, except for the serial collector, only it can work with the CMS collector at this time.
The Parnew Collector is also the default Cenozoic collector after you use the-XX:+USECONCMARKSWEEPGC option, or you can use the-XX:+USEPARNEWGC option to force it to be specified.
The number of collection threads that are turned on by default is the same as the number of CPUs, and in a very large CPU environment, you can use the-xx:parallelgcthreads parameter to limit the number of threads that are garbage collected.

The ③parallel scavenge collector Parallel Scavenge collector is a new generation collector, it is also a collector using the copy algorithm, and a parallel multi-threaded collector
The goal of the Parallel scavenge collector is to achieve a controllable throughput (throughput)
The Parallel scavenge collector provides two parameters for precise control of throughput, respectively, the-xx:maxgcpausemillis parameter that controls the maximum garbage collection pause time, and the-xx:gctimeratio parameter that directly sets the throughput size.
The value allowed for the Maxgcpausemillis parameter is a number of milliseconds greater than 0, and the collector will try to ensure that the memory collection takes no longer than the set value.
Less pauses, more frequent garbage collection, and throughput down the parallel scavenge collector also has a parameter-xx:+useadaptivesizepolicy worth paying attention to. This is a switch parameter, when this parameter is opened, there is no need to manually specify the size of the Cenozoic (-XMN), the ratio of Eden to Survivor Area (-xx:survivorratio), the age of promotion to the old age (-xx: Pretenuresizethreshold), the virtual opportunity collects performance monitoring information according to the current system operation, dynamically adjusts these parameters to provide the most suitable pause time or maximum throughput, which is called the GC Adaptive Tuning Strategy (GC Ergonomics).
The adaptive Tuning strategy is also an important difference between the parallel scavenge collector and the Parnew collector.

The ④serial old collector, Serial, is an older version of the Serial collector, and it is also a single-threaded collector, using the "mark-and-organize" algorithm.
The main meaning of this collector is also to use the virtual machine in client mode. In the case of server mode, it has two main uses: one for use with the parallel scavenge collector in JDK 1.5 and earlier, and the other for a backup of the CMS collector, which takes place in a concurrent collection concurrent used when Mode failure.

The ⑤parallel old collector, Parallel, is an older version of the Parallel scavenge collector, using multithreading and the "mark-and-organize" algorithm.
The parallel scavenge plus parallel old collector is a priority when it comes to throughput and CPU resource sensitivity.

The ⑥cms collector cms (Concurrent Mark Sweep) collector is a collector with the goal of obtaining the shortest recovery pause time. (Application of Internet application, b/s system service, etc. which pay attention to the response speed)
The CMS collector is implemented based on the "tag-purge" algorithm, which is more complex than the previous collectors, and consists of 4 steps, including: initial tag (CMS initial mark) concurrency token (CMS concurrent Mark) re-tagging (CMS remark) concurrent Purge (CMS concurrent sweep) initial tag, re-tagging these two steps still require "Stop the World" (Pause other threads)
The initial tag simply marks the object that the GC Roots can directly relate to, fast, and the concurrent tagging phase is the process of GC Roots tracing. The re-tagging phase is to fix the tag record of the part of the object that caused the tag to change during the concurrent tag because the user program continues to work

Benefits of the CMS: concurrent collection, low pauses, also known as the concurrent low-pause collector (Concurrent-lo pause Collector). It has the following 3 obvious drawbacks:
    • The CMS collector is very sensitive to CPU resources. the number of recycled threads that the CMS starts by default is (CPU number +3)/4, that is, when the CPU is over 4, the garbage collection thread is not less than 25% of the CPU resources when it is reclaimed concurrently, and decreases as the number of CPUs increases. However, when the CPU is less than 4 (for example, 2), the impact of the CMS on the user program can become very large.
    • The CMS collector is unable to handle floating garbage (floating garbage), and a "Concurrent Mode Failure" failure may occur resulting in another full GC.
because the user thread still needs to run during the garbage collection phase, there is also a need to reserve enough memory space for the user thread to use, so the CMS collector cannot wait until the old age is almost completely filled up like the other collectors and then collects it, and needs to reserve a portion of the space for the program to run when it is collected concurrently. In the default settings of JDK 1.5, the CMS collector will be activated when the old age uses 68% of the space, this is a conservative setting, if the old age in the application is not too fast, you can properly adjust the parameter-xx: The cmsinitiatingoccupancyfraction value to increase the trigger percentage to reduce the number of memory recoveries for better performance.
If the memory reserved during the CMS operation does not meet the needs of the program, a "Concurrent Mode Failure" failure occurs, and the virtual machine will start a fallback plan: temporarily enable the serial old collector to re-age garbage collection, so the pause time is very long. So the parameter-xx:cmsinitiatingoccupancyfraction set too high can easily lead to a lot of "Concurrent Mode Failure" failure, performance is reduced.
    • At the end of the collection there will be a lot of space debris.
The CMS Collector provides a-xx:+usecmscompactatfullcollection switch parameter (which is on by default) for the merge process to turn on memory fragmentation when the CMS collector is unable to FULLGC
Parameter-xx:cmsfullgcsbeforecompaction, this parameter is used to set the number of times to execute a full GC without compression, followed by a compressed (the default is 0, which means that every time you enter the full GC is defragmented).

Features of the ⑦G1 (garbage-first) collector G1 Collector
    • parallel and concurrency: G1 can take advantage of the hardware benefits of multi-CPU, multi-core environments, and use multiple CPUs (CPU or CPU cores) to reduce the time of Stop-the-world pauses , some other collectors would have to pause the GC action performed by the Java thread, and the G1 collector would still be able to continue executing the Java program in a concurrent manner.
    • generational collection: As with other collectors, generational concepts remain in the G1. Although G1 can manage the entire GC heap independently without the need for other collector mates, it can handle newly created objects in different ways and old objects that have survived for a period of time to get better collection results.
    • space consolidation: Unlike the CMS's "tag-clean" algorithm, G1 is generally based on the "tag-organize" algorithm implementation of the Collector, From the local (two region) is based on the "Replication" algorithm implementation, but in any case, both of these algorithms means that G1 operation will not generate memory space fragmentation, collected after the provision of regular free memory. This feature is useful for long-running applications where large objects are allocated without triggering the next GC ahead of time because of the inability to find contiguous memory space.
    • Predictable pause: This is another big advantage of G1 relative to the CMS, reducing the pause time is a common concern for G1 and CMS, but G1 in addition to the pursuit of low pauses, It is also possible to establish a predictable pause-time model, which allows the user to explicitly specify that the time spent in a time fragment of M milliseconds is less than n milliseconds spent on garbage collection, which is almost a feature of the real-time Java (RTSJ) garbage collector.
The G1 collector is able to establish a predictable pause-time model because it can plan to avoid full-area garbage collection throughout the Java heap. G1 tracks the value of the garbage accumulation in each region (the amount of space recovered and the amount of time it takes to recover), maintains a prioritized list in the background, and each time according to the allowable collection times, Prioritize the recovery of the region with the greatest value (this is the reason for the Garbage-first name). This uses the region to divide the memory space and has the priority area recovery method, guaranteed the G1 collector to obtain the highest collection efficiency in the limited time.
In the G1 collector, the object reference between region and the new generation to the old age in other collectors, the virtual machine uses remembered set to avoid a full heap scan.

If you do not calculate the operation of maintaining remembered set, the operation of the G1 collector can be broadly divided into the following steps:initial tag (Initial marking),concurrency Tokens (Concurrent marking),final mark (final marking),Filter collection (Live Data counting and evacuation)The initial marking phase simply marks the object that GC roots can directly relate to, and modifies the value of Tams (next Top at Mark Start), allowing the next stage of the user program to run concurrently, creating new objects in the correctly available region, which requires a stalled thread. But it takes a short time. The concurrency tagging phase begins with the GC root to perform a scalability analysis of objects in the heap, identifying the surviving objects, which take a long time but can be executed concurrently with the user program. The final marking phase is to fix the tag record that is causing the markup to change as the user program continues to work during the concurrency tag, and the virtual machine will record the time object changes in the thread remembered set logs, and the final marking phase requires the remembered set Logs data is merged into the remembered set, which requires a stalled thread, but can be executed in parallel. Finally in the filter recovery stage first of each region of the recovery value and cost of sorting, according to the user's desired GC pause time to make a recycling plan, from the information disclosed by Sun, this stage can actually be implemented concurrently with the user program, but because only a portion of the region , time is user-controllable, and pausing the user thread will significantly improve collection efficiency.


⑧ Garbage Collector Parameters Summary

6 The memory allocation and reclamation policy objects are primarily allocated on the new generation of Eden, and if a local thread allocation buffer is started, it will be allocated on Tlab by thread precedence. In a few cases it may also be assigned directly in the old age, the allocation rules are not completely fixed, the details depend on which garbage collector combination is currently in use, and the settings of the memory-related parameters in the virtual machine.

The ① object takes precedence over the Eden Allocation generation GC (Minor GC): Refers to the garbage collection action that occurs in the Cenozoic, because most Java objects have the characteristics of being born and going out, so Minor GC is very frequent and the general recovery rate is faster. Old age GC (Major gc/full GC): Refers to the GC, which occurred in the old age, Major GC, often accompanied at least once minor GC (but not absolute, in the parallel scavenge collector's collection strategy is directly Major GC's policy selection process). Major GC is typically 10 times times slower than the minor GC.
② large object directly into the old age pretenuresizethreshold parameter is only valid for serial and parnew two collectors, Parallel scavenge collector does not recognize this parameter, Parallel Scavenge collectors generally do not need to be set up. If you encounter an occasion where this parameter must be used, you can consider Parnew plus the collection of CMS combinations.

③ the long-term survival of the object will enter the old age if the object is still alive after Eden was born and after the first minor GC, and can be accommodated by survivor, it will be moved to the survivor space, and the object age is set to 1. Objects in the Survivor area each "through" a minor GC, age increased by 1 years, when its age increased to a certain extent (by default, 15 years old), will be promoted to the old age. The age threshold at which an object is promoted to the old age can be set by parameter-xx:maxtenuringthreshold.

④ Dynamic Object age negotiation in order to better adapt to the memory status of different programs, the virtual machine is not always required to reach the age of the object must be maxtenuringthreshold to promote the old age, If the sum of all objects of the same age in the survivor space is greater than half the size of survivor space, objects older than or equal to that age can enter the old age without waiting for the age required in Maxtenuringthreshold.

⑤ space allocation guarantee before the minor GC occurs, the virtual opportunity checks whether the largest available contiguous space in the old age is greater than the total space of all new generation objects, and if this condition is true, then the minor GC can ensure that it is safe. If not, the virtual opportunity to see if the Handlepromotionfailure setting value allows the warranty to fail. If allowed, then will continue to check whether the largest available continuous space in the old age is greater than the average size of the previous promotion to the old age object, if greater than, will try to do a minor GC, although this time the minor GC is risky; or the Handlepromotionfailure setting does not allow for adventure, then it is also time to perform a full GC instead.

7. The summary of memory recovery and garbage collector is one of the main factors that affect system performance and concurrency, and the virtual machine provides many different collectors and provides a large number of tuning parameters, because the best performance can be obtained only by choosing the optimal collection method according to the actual application requirement and implementation mode.

From for notes (Wiz)

003 Garbage collector and memory allocation policy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.