Java Virtual machine Learning: generational collection algorithms

Source: Internet
Author: User

absrtact: The current commercial virtual machine garbage collection uses the "generational collection" (generational Collection) algorithm, this algorithm does not have any new idea, just according to the object's survival period of different memory divided into several pieces. 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 of the high survival rate of the object and the lack of additional space to guarantee it, it must be recycled using the "mark-clean" or "mark-sweep" algorithm.

Category of Objects

The generational collection algorithm is based on the different characteristics of the object, and using the appropriate algorithm, there is no actual new algorithm generated. It is not so much a fourth algorithm as a generational collection algorithm that it is a practical application to the first three algorithms.
First of all, let's explore the different characteristics of the object, then LZ and everyone to choose the GC algorithm for these objects.
In-memory objects can be roughly divided into three of the life cycle, and the following names are personal names.
1, the death of the object: the object of the XI-sheng, popular point is to live not long to die of the object.
Example: Local variables for a method, temporary variables within a loop, and so on.
2, the old Immortal object: This kind of object generally live relatively long, the age is very big still not dead, but in the final analysis, the old immortal object also almost sooner or later die, but also just almost.
Examples: Cache objects, database connection objects, singleton objects (singleton mode), and so on.
3, non-extinguishing objects: such objects generally once born almost undead, they will almost always immortal, remember, just almost immortal.
Example: The object in the string pool (enjoy meta mode), the loaded class information, and so on.

the area of memory that the object corresponds to

Do you remember how the JVM divided the memory when I described memory management earlier?
We mapped the above three objects to the memory area, that is, the aborted object and the old undead object are all in the Java heap, and the object is in the method area.
As we have already said in the previous chapter, for the Java heap, the JVM specification requires that the GC be implemented, so that death is almost inevitable for dead objects and old undead objects, but it is only nearly, and inevitably, that some objects 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 a GC for the method area, then the non-extinguished object is a true non-extinguished object.
Because the life cycle of an Immortal object is too long, the generational collection algorithm is designed for the Java heap, that is, for dead objects and old undead objects.

Object Recycling for Java heap (aborted objects and old undead objects)

With the above analysis, we will look at how the generational collection algorithm handles the memory recovery of the Java heap, that is, the recovery of the dead object and the old object.
Dying object: This kind of object is going to live and dying, survival time is short, remember the use of the replication algorithm requirements? That is, the survival rate of the object is not too high, so the aborted object is best suited for using the replication algorithm.
Small question: 50% How to waste memory?
Answer: Because the dead object generally has a low survival rate, so can not use 50% of memory as Idle,In General, two blocks of 10% of memory are used as idle and active intervals, while another 80% of the memory is used to allocate memory to new objects. Once a GC occurs, 10% of the active interval is transferred to the remaining 80% of the surviving objects to 10% of the free interval, then the previous 90% of the memory is released, and so on.

GC Process:

The figure highlights the situation of the respective memory in each of the three regions at each stage. Believe that looking at the diagram, its GC process is not difficult to understand.

The 1th is to use this way, we only waste 10% of the memory, this is acceptable, because we swapped the memory neatly arranged with the GC speed. 2nd, the premise of this strategy is that each surviving object will not occupy more than 10% of the size of the memory, and once exceeded, the extra object cannot be copied.

In order to solve the above unexpected situation, that is, the survival of the object occupied by the memory is too large, the master will divide the Java heap into two parts to deal with, the above three areas is the first part, called the new generation or young generation. The remaining part, which is devoted to storing old undead objects, is called the old age generation.

Is it an apt name? Let's look at the way the old undead are handled.

Old Undead: This type of object survival rate is very high, because most of them are transferred from the Cenozoic. Just like people, live years long, become old and not die.

Typically, when the following two cases occur, the object is transferred from the Cenozoic region to the old band area.
1, in the new generation of each object, there will be an age, when the age of these objects reached a certain level (age is the number of GC, each time the GC if the object survived, then the Age plus 1), it will be transferred to the old generation, and this into the old age of the value, generally in the JVM can be set.
2, when the new generation of living objects occupy more than 10% of memory, then the extra objects will be put into the old generation. At such times, the old generation is the "spare depot" of the new generation.
For the characteristics of an old undead object, it is obviously no longer appropriate to use the replication algorithm, because it has a high survival rate, and do not forget that if the old generation reuse the replication algorithm, it does not have a spare warehouse. Therefore, in general, the old undead object can only be tagged/collated or marked/cleared.
The above two cases have solved most of the GC problem, because the Java heap is the main object of the GC, and the above also contains the entire content of the collection algorithm, the next for the non-destroyed object recycling, has not belonged to the generation of collection algorithm content.
object Reclamation for method area (non-extinguished object)

An Immortal object exists in the method area, and in our usual hotspot virtual machine (the JDK default JVM), the method area is also affectionately referred to as the permanent generation, and is a very apt name, isn't it?
In fact, a long time ago, there is no permanent generation. At that time, the permanent generation and the old generation were stored together, which contained the Java class instance information and the class information. However, it was discovered that the unloading of class information almost rarely occurred, thus separating the two. Fortunately, this does improve a lot of performance. The permanent generation was then split.
The GC in this part of the area uses a similar approach to older generations, since there are no "spare warehouses", both of which can only use the mark/Purge and mark/collate algorithms.

Timing of recycling

The JVM does not collect each of the above three memory regions each time it performs GC, and most of the time it refers to the new generation. So the GC has two different types in the reclaimed area, one for the normal GC (minor GC) and one for the global GC (major GC or full GC), and they are for the following areas.
Normal GC (minor GC): GC for Cenozoic regions only.
Global GC (major GC or full GC): GC for all generational regions (Cenozoic, old generation, permanent generations).
Because the GC effect is not good for older generations versus permanent generations, and the memory usage of both is slow, in general, it takes several regular GC to trigger a global GC.


Java Virtual machine learning: Generational collection algorithm

Related Article

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.