Introduction
What is the ultimate algorithm?
In fact, the current JVM uses the algorithm, is not really the ultimate. Maybe a few years later, there will be a new ultimate algorithm, and almost certainly, because LZ believe in the ability of people.
So what does a generational collection algorithm do with GC?
Object classification
As has been said in the previous chapter, the generational collection algorithm is based on the different characteristics of the object, and uses the appropriate algorithm, which does not have the actual new algorithm. Rather than the fourth algorithm, the algorithm is a practical application of the first three algorithms.
First, let's look at the different characteristics of the object, and then the LZ and you all come together to select the GC algorithm for these objects.
The objects in memory can be roughly divided into three kinds according to the length of the life cycle, and the following names are the names of LZ individuals.
1, the death of the object: the object of the dying, popular point is not live long to die of the object.
Example: a local variable of a method, a temporary variable within a loop, and so on.
2, Old Undead object: This kind of object generally live relatively long, the age is very big still not dead, but in the final analysis, the old undead object also almost sooner or later die, but also just almost.
Examples: cached objects, database connection objects, single instance objects (single case mode), and so on.
3, do not kill the object: Such objects are generally almost immortal once born, they will almost always immortality, remember, but almost never extinguished it.
Example: Objects in a string pool (the mode of entitlement), loaded class information, and so on.
The memory area corresponding to the object
Remember how the JVM divided memory when it was described in the previous memory management?
We will correspond to the above three objects to the memory region, that is, aborted objects and old undead objects are in the Java heap, but not the object in the method area.
As we've already said in the previous chapter, for Java heaps, the JVM specification requires that GC be implemented, so for dead objects and old undead, death is almost inevitable, but it's just that there are still some objects that will survive until the end of the application. However, the JVM specification does not require a GC for the method area, so if a JVM implementation does not implement GC for the method area, then the object is really not extinguished.
Because the life cycle of the object is too long, the collection algorithm is designed for the Java heap, which is aimed at the aborted object and the old undead object.
Object Recycling for Java heaps (aborted objects and old undead objects)
With the above analysis, we take a look at how the generational collection algorithm handles the memory recovery of the Java heap, which is the recovery of aborted objects and old undead objects.
Aborted object: This kind of object faces to live, the survival time is short, still remember the use of the copy algorithm request? That is, the object survival rate is not too high, so the aborted object is the most suitable to use the replication algorithm.
Small question: 50% memory waste how to do?
Answer: Because aborted object general survival rate is low, so can not use 50% of memory as idle, general, use two block 10% of memory as idle and activity interval, and another 80% of memory, is used to allocate memory for new object. Once the GC occurs, the 10% activity interval is transferred to the 10% free interval from the remaining 80%, and then the previous 90% memory is released, and so on.
In order for you to see this GC process more clearly, LZ gives the following illustration.
The figure is marked with three regions in each phase, the respective memory situation. It is understandable that the GC process is not difficult to understand.
But there are two points LZ need to mention, the 1th is to use this way, we only waste 10% of the memory, this is acceptable, because we swapped the memory of the neat arrangement and GC speed. 2nd, the premise of this strategy is that each surviving object will not be able to occupy more than 10% of the size of the memory, once more than the number of objects can not be copied.
In order to solve the above accident, that is, the memory of the surviving object is too large, the master of the Java heap is divided into two parts to deal with, the above three regions is the first part, called the new generation or young generation. And the remainder, which specializes in storing old and dead objects, is called the old generation.
Is it an apt name? Let's look at how the old undead object is handled.
Old Undead: This kind of object has a very high survival rate because most of them are transferred from the Cenozoic. Just like a man, the life of a long time, become old undead.