Java GC mechanism and memory allocation policy

Source: Internet
Author: User

The collection algorithm is the method of memory recycle, and the garbage collector is the concrete implementation of memory recycle.

Automatic memory management solves: allocating memory to objects and reclaiming memory allocated to objects

Why should we learn to learn about GC and memory allocation?
With the help of the JVM's automatic memory management mechanism, it is no longer necessary to write a paired delete/free code for each new operation. However, if you do not know how the virtual machine uses memory when there is a memory leak and overflow problem, troubleshooting will be a very difficult task.

The GC (garbage collector) determines which objects are "alive" and which are "dead" before they are reclaimed by the heap. Then there is the object Survival decision algorithm .

An algorithm for object Survival Determination Reference counting algorithm:

Algorithm idea: To add a reference counter to the object, whenever there is a place to reference it, the counter value plus 1, when the reference fails, the counter value minus 1, any time the counter is 0 of the object is impossible to be used again.
Advantages: Simple implementation, high efficiency of judgment
Disadvantage: It is difficult to solve the problem of circular referencing between objects

Accessibility Analysis algorithm:

Algorithm idea: Through a series of "GC Roots" object as the starting point, starting from these nodes to search down, the path of the search is called the reference chain, when an object to the GC Roots no reference chain connected, it proves that this object is not available.

Although Object5, OBJECT6, and OBJECT7 are related to each other, they are not accessible to GC roots, so they will be judged as recyclable objects.

The objects that can be used as GC Roots include the following:
1. Objects referenced in the virtual machine stack
2. Objects referenced by class static properties in the method area
3. Objects referenced by constants in the method area
4. Objects in the local method stack that are referenced by JNI

To survive or to die?

Even those unreachable in the Accessibility analysis algorithm are not "immortal", 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 finds no reference chain connected to the GC roots after the accessibility analysis, it will be first tagged and filtered, and the condition is whether this object is necessary to execute the Finalize () method, and when the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine, and 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, where the so-called "execute" 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 occurs, it will likely cause other objects in the F-queue queue to be permanently waiting. It even led to the collapse of the entire memory recovery system.
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, and if the object is to successfully save itself in the Finalize () method, simply 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 "About to be recycled" at the second mark, and if the object has not escaped at this time, it is basically recycled.

In addition, the Finalize () method of any object is automatically called only once by the system. Finalize () can do all the work, use try-finally or other methods can do better, more timely, so it is recommended that you can completely forget the Java language has this method exists. See the deep understanding of Java virtual machines

garbage Collection Algorithm tag-purge algorithm:

The algorithm is divided into two stages: "Mark" and "purge".
All objects that need to be recycled are marked first, and all tagged objects are collected uniformly after the tag is completed
Deficiencies:
1. Efficiency issues, marking and clearing two processes are not efficient.
2. Space problem: After the mark is cleared, there is a large amount of discontinuous memory fragmentation, too much space fragmentation can lead to the subsequent need to allocate large objects in the course of the program running, unable to find enough contiguous memory and have to trigger another garbage collection action in advance

Replication algorithm:

Algorithm implementation: Divide 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, 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 moving the heap top pointer, sequentially allocated memory. Easy to implement and efficient to run. The cost of the algorithm is to say that the memory is reduced to half the original, it is too high point.

tag-Collation algorithm:

Algorithm implementation: Mark all objects that need to be recycled, and let all surviving objects move toward one end. Then directly clean out the memory outside the end boundary.

Generational collection algorithm:

Algorithm implementation: According to the different life cycle of the object divides the memory into several blocks, generally divides the Java heap into the new generation and the old age, so can use the most appropriate collection algorithm according to the characteristic of each age.
In the Cenozoic, every garbage collection found that a large number of objects died, only a small number of survival, then choose the replication algorithm. The collection can be done with only a small amount of replication costs for the surviving objects.
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.

Cenozoic: divided into three spaces, one Eden space and two survivor spaces.
The vast majority of newly created objects are assigned here, and many objects are created in the new generation and disappear as most objects become inaccessible soon after they are created. The process by which objects disappear from this area is what we call "minor GC".

old age: objects have not become unreachable and survived from the Cenozoic and are copied here. It occupies more space than the Cenozoic. Because of its relatively large space, the GC that occurs in the old age is much less than that of the Cenozoic. The process of disappearing an object from the old age, which we call "major GC" (or "full GC")

The vast majority of objects that have just been created are stored in Eden space. After the first GC has been performed in Eden Space, the surviving objects are moved to one of the survivor spaces. Thereafter, after the GC is executed in Eden Space, the surviving objects are stacked in the same survivor space. When a survivor space is saturated, the surviving object is moved to another survivor space. It will then empty the survivor space that is already saturated.
In the above steps, repeated several times the surviving objects will be moved to the old age.

garbage collector

is a garbage collector that acts on different generations, and if there is a connection between the two collectors, it can be used in combination. The region in which the virtual machine resides indicates whether it belongs to the new generation collector or the old collector.

Learn about the "Stop the World" before learning a variety of garbage collectors. "Stop the World" will occur in any GC algorithm. "Stop the World" means that the JVM stops executing the application because it is performing a GC. When "Stop the World" occurs, all threads are waiting in addition to the GC's required threads until the GC task completes. GC optimization is often referred to as reducing the time that "Stop the World" takes place.
"Stop the World" This understanding is very image: Your mother when you clean the room, it will certainly let you honestly in the chair or outside the room to wait, if she cleans, you throw scraps of paper, this room can be cleaned?

Serial collector: single thread, Cenozoic collector, using copy algorithm. It will only use a CPU or a collection thread to complete the garbage collection work, in the garbage collection, must "Stop the world", suspended for all of his work threads until it collects the end.

parnew Collector: Multi-threaded versions of the serial collector, control parameters, collection algorithms, Stop the world, object assignment rules, and recycling policies are all identical to the serial collector

Parallel Scavenge Collector: generation collector, using copy algorithm, parallel multithreading.

Serial old collector: The old version of the serial collector, single threaded, uses the tag-collation algorithm.

Parallel old collector: Older versions of the Parallel scavenge collector, multi-threading, using the tag-collation algorithm

CMS collector: A collector that targets the shortest recovery pause time, based on the "tag-purge" algorithm. The operating process is divided into four steps: initial tag, concurrency token, re-tagging, concurrency cleanup.
The initial tag, re-tagging these two steps still need "Stop the World". The initial tag simply marks the object that the GC roots can directly relate to and is fast. The concurrent tagging phase is the process of GC Roots tracing, while the re-tagging phase is to fix the tag record of the part of the object that caused the markup to change as the user program continues to work during the concurrency tag, which is typically slightly longer than the initial marking stage, But far more than the time period of the concurrent tag.
The most time-consuming concurrency token and concurrent cleanup process throughout the process, the collector thread can work with the user thread, so the memory recycling process for the CMS collector is performed concurrently with the user thread.
Pros: Concurrent collection, low pauses
Cons: Very sensitive to CPU resources, unable to handle floating garbage, based on the tag cleanup algorithm, there is a lot of control fragmentation at the end of the collection

G1 Collector: The G1 collector is one of the most cutting-edge achievements in the development of collector technology today, a garbage collector for service-side applications.
G1 features: Parallel and concurrency, generational phones, spatial integration, predictable pauses
The operation process is as follows: initial tag, concurrency tag, final tag, filter collection.
The initial tagging phase simply marks the object that GC roots can directly relate to, and modifies the value of Tams to allow the next user program to run concurrently, creating a new object in the correctly available region, which requires a stalled thread, but takes a short time.
The concurrency tagging phase is a time-consuming process that can be performed concurrently with the user program, starting with a roots analysis of the objects in the heap from GC to find the surviving objects.
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 this time object change in the thread remembered Set logs. The final tagging phase requires merging data from the remembered set logs into the remembered set, which requires a stalled thread but can be executed in parallel.
Finally, the recovery value and cost of each region are sorted first in the filter recovery phase. A recovery plan is developed based on the expected GC pause time for the user.

memory allocation and recovery policy

objects take precedence over Eden Allocations: In most cases, objects are allocated in the New Generation Eden area. When the Eden Zone does not have enough space to allocate, the virtual machine initiates a minor GC.

large objects go straight into the old age: Large objects are Java objects that require a large number of contiguous memory controls, the most typical of which are long strings and arrays.

long-lived objects will enter the old age: virtual machines Use the idea of generational collection to manage memory, so memory recycling must be able to identify which objects should be placed in the new generation and which should be in the old age. To do this, the virtual machine defines an object age counter for each object. 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 survivor space, and the object age is set to 1, the object in the survivor area each "survived" a minor GC, age increased by 1 years, When its age increases to a certain level (by default, 15 years old), it will be promoted to the old age.

Dynamic Object Age judgment: virtual machines do not always require that the age of the object must reach Maxtenuringthreshold in order to be promoted to the old age, if the sum of all objects of the same age in Survivor space is greater than half the survivor space, Objects older than or equal to that age can go straight into the old age without waiting for the ages required in the maxtenuringthreshold.

Space Allocation Guarantee: before the minor GC occurs, the virtual opportunity first 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 be guaranteed to be secure. If not, the virtual opportunity to see if the Handlepromotionfailure setting value allows the warranty to fail.

Summary

Memory recycling and garbage collector is one of the main factors that affect system performance and concurrency, so 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. There is no fixed collector, parameter combination, there is no optimal tuning method, the virtual machine will not have any inevitable memory recovery behavior.

The above is my study of "in-depth understanding of Java Virtual Machine" a book to organize notes.

Reference "in-depth understanding of Java virtual machines"

Java GC mechanism 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.