First, the introduction
In the previous blog "jvm--Introduction" in the main introduction of the JVM memory model, think about:
Why divide the heap, stack, method area, etc.?
Why are different types of data stored separately?
The answer can be broken down into a lot of strips, and here's one aspect, if we're going to distinguish the kind of data, that's the scope. For example: Heaps, method areas are thread-shared, and stacks are private.
So what are the aspects of management? Include Create, store, recycle? This blog is about garbage collection (garbage Collection).
I suggest that you as a GC, the recycling of garbage for the work of people, so it seems a bit ...
Second, the algorithm
Ask yourself three questions:
what--Recycling What
how--How to Recycle
When is when--recycled?
1. What
What are we going to recycle as a GC? object instances, variables, type information ... How do you know which ones to recycle and which do not need to be recycled, and whether the object instance is still applied, is this object dead?
Judging an object is dead, listening is very simple, doing it is not so the same thing.
1) counter
Bind a counter to each object instance, and if someone references him, add 1, minus 1 after the reference is no longer quoted. Then the duty is 0 o'clock, should be not used.
The implementation is simple, the efficiency is also high, but cannot solve the circular reference question. That a refers to b,b reference A, according to this idea, they will be permanently tied together.
2) Reference chain
Suppose there is a root node, as a starting point, to search down, when an object is not on this chain, that is why he is not able to go to root, we say that he is no longer used, can be recycled.
Too lazy to draw, from the Internet to find a picture.
2, How1) mark-Clear
Two stages: Mark first, and then collect the uniform.
This method is very simple and basic, but it is not very optimistic. Time, two process efficiency is not high, space, will produce a lot of free debris, not conducive to re-use.
So what? Look at the following method to improve him.
2) Copy
Mainly solve the efficiency problem.
Divide the memory into two areas of the same size, using only one block. When this piece of memory is exhausted, copy the object to the unused area of memory and clean it up.
The shortcomings of the obvious, memory is very valuable AH!!! This is the Achilles heel of the replication algorithm,
The estimation of this idea is a rich son who has not been poor.
3) Marking-finishing
The main solution is the memory fragmentation problem.
The first half of his process is the same as the mark-sweep, which, after cleanup, moves the surviving object to one end and solves the problem of fragmentation.
Of course, efficiency is definitely not marked-cleared, but balanced, this algorithm is good.
3. When
GC does not accurately control the exact time of collection, but generational collection can control the frequency of a collection.
To start with, the article begins by saying that the data scopes (lifecycles) that are stored in the heap and stack are different, so they are certainly not the same. In fact, to be more complicated, the object instances within the heap have different time to live, and if each recovery is scanned once, then the efficiency is very low. Based on this, the well-known generational collection algorithm is introduced.
Collection of generations
Broadly divided into three eras: The new generation, the old age and the permanent generation. In the Cenozoic, two regions were divided: S0 (Survivor0), S1 (Survivor1). Individual names may be translated differently, and understanding is good.
Cenozoic
Eden: The Meaning of Eden, where newly created objects are generally stored. These objects have two endings, either collected or removed, or moved to the next survivor space.
Survivor: Survivors, roughly speaking, they have survived a death.
Old age
Old Space: When the object reaches a certain percentage in the Cenozoic, it moves the extra objects into the older generation.
Permanent generation
The first two kinds are stored in the heap, so you can think of the old age as a new generation of "spare warehouse." And the permanent generation is in the method area, the recovery frequency is the slowest.
Each era has its own characteristics, and they can choose their own algorithms for recycling. The number of recoveries per generation is large, and replication algorithms can be used. The old age object survived long and space is not big, can only use "mark-clean" or "mark-organize".
The generational collection algorithm actually uses several basic algorithms in how, only divides the region (the age), the more scientific use collects the algorithm.
Third, collector
In front of the garbage collection to solve what, how, when the problem, then start real deal work! Who's going to do it? How do you do it?
The collector is to help us to solve this problem, each feature is not the same, in a separate introduction, we first to classify.
Standard |
Collecting Device |
Age |
New generation, old age |
Working mode |
Serial, parallel, concurrency |
Fragment processing |
Compressed, non-compressed |
...... |
...... |
There is no best collector, there is no universal collector, only the choice is more suitable is scientific.
1) Serial
Adj.
1. A succession of; a series of.
2. The periodical publication, (fiction, etc) serial, continuous publishing, continuous broadcasting.
3. Payment by instalments.
4. The "Computer" in a row;
Characteristics:
Simple
Single thread;
Cenozoic.
2) parnew
Multi-threaded version of serial;
Cenozoic
3) Parallel Scavenge
Parallel
Adj.
1. Parallel, parallel (to, with); "Electricity" in parallel.
2. In the same direction, for the same purpose.
3. Same, same, similar, corresponding.
Cenozoic
Parallel
Pay attention to throughput (this is said later).
4) CMS
Concurrent low pause Collector concurrency lower pause collector
Pay attention to the pause time, fast response, bring the user a good experience.
The process is more complicated, the space is limited, no longer introduced.
5) G1
Concurrent
Generation of collection;
Labeling-finishing;
Reduce the pause time.
Classification
Here are just a few of the more typical collectors, from an age perspective:
Note: The above line is the new generation, the following is the old age. The connection between the collectors indicates that they can be used with each other.
Summary:
The new generation basically uses the replication algorithm, the old age uses the marker collation algorithm.
Both algorithms solve the problem of fragmentation, because the new generation of space is larger, can be replicated, more efficient.
In terms of how it works:
Serial
When garbage collection, "Stop the World" pauses all threads. such as: serial/serial old
Parallel
Multiple threads can be executed at the same time, suitable for multiple CPUs. such as: Parallel scavenge
Concurrent
Partial collection work can be performed alternately with the user thread. such as: CMS
The serial is only suitable for small data volume, which is high CPU usage and efficiency.
How do you pick the right collector?
This can be considered from the following aspects:
Client mode: Prioritize the user experience, respond quickly, and pause for a long time.
Server mode: Throughput priority, more efficient use of the CPU, as soon as possible to complete the task, suitable for background operations. throughput = Run code time/(run code +GC).
JVM (2)--GC Algorithms and Collectors