Reprinted: JVM memory Generational Strategies

Java virtual machines based on different objects survival period, the heap memory is divided into several pieces, generally divided into the new generation, years old and permanent generations (in terms of the HotSpot VM), which is the JVM memory generational strategy.

       Why generational?

       Heap memory is the biggest piece of virtual machine memory management, garbage collection is the most frequent an area, we program all object instances are stored in the heap memory. A generational heap memory in order to improve the object memory allocation and garbage collection efficiency. Imagine if the heap memory is not zoning, all newly created objects and object life cycle is very long together, as the program executes, heap memory require frequent garbage collection, and each must be recovered through all objects, traversal of these objects takes time cost is enormous, it will seriously affect our GC efficiency, which is simply terrible.

       With the generational memory, the situation is different, newly created objects will allocate memory in the new generation, after several recovery still surviving objects stored in the old era, static property, class information stored in the permanent generation, the new generation of objects in the survival time is short, only need to GC frequent in the new generation area, old era object life cycle is long, the frequency of garbage collection is relatively low, does not require frequent recycling, recovery of the permanent generation is very poor, generally not garbage collection, it can also use the appropriate garbage collection algorithm based on the characteristics of different ages. Generational collection greatly enhance the collection efficiency, these are the benefits brought about generational memory.

       Memory generational divide

      Java virtual machine heap memory is divided into the new generation , the old year and permanent generations , is a unique concept of permanent generation of HotSpot virtual machine, which uses a permanent way to achieve generation method area, no other virtual machine implementations of this concept, and HotSpot there are also canceled permanently on behalf of the trend in the HotSpot JDK 1.7 has already begun "to perpetuate", originally on behalf of the permanent removal of a string constant pool. Permanently store data on behalf of the main constants, class information, such as static variables, little to do with garbage collection, the new generation and the old year is the main area for garbage collection. Memory generational diagram is as follows:

 

       

       The new generation (Young)

       The new generation of priority objects stored in the new generation, the new generation of students toward the object evening died, the survival rate is very low, in the new generation, conventional applications can be recycled garbage collection generally 70% to 95% of the space, high recovery efficiency.

       The HotSpot new generation is divided into three, a larger and two smaller Eden space Survivor space, the default ratio of 8: 1: 1. HotSpot object divided due to use of the new generation to recover the replication algorithm, the ratio is set to take advantage of memory space, reduce waste. The new generation of object allocations in the Eden area (except for large objects, large objects directly into the old year), when the Eden area there is not enough space allocated, the virtual machine will launch a Minor GC.

       GC starts when the object is present only in the region and Eden From Survivor region, To Survivor empty area (a reserved area). When GC carried out, Eden District, all live objects are copied to the To Survivor region, while in From Survivor region, still live objects will decide fate according to their age values, the age value reaches the age threshold (default is 15, each new generation of objects to get through a garbage collection, age, value is added by 1, GC generational age of objects stored in the header object) will be moved to the old era, did not reach the threshold of the object will be copied to the To Survivor region. Then empty Eden From Survivor region and area, the new generation of live objects are To Survivor region. Then, From Survivor district and To Survivor area will exchange their roles, that is, the new district is the last GC To Survivor emptied From Survivor district, the new district is the last GC From Survivor To Survivor's district, in short, in any case To Survivor will ensure the area after a GC is empty. When To Survivor target area did not survive the new generation of space to store a collection down the GC, you need to rely on old's allocation guarantees, these objects stored in the old era.

       Years old (Old)

       After a number in the new generation (see the specific thresholds virtual machine configuration) GC still surviving objects into the old era. The old era of object life cycle is longer, the survival rate is relatively high, relatively low frequency of GC in the old era, but the recovery pace is relatively slow.

       Permanent Generation (Permanent)

       On behalf of permanent storage class information, constants, static variables, the time compiler to compile the code and other data for the region, Java Virtual Machine Specification pointed out that garbage collection can not, in general, will not be garbage collected.

       Minor GC and the difference of Full GC

       New Generation GC (Minor GC): Minor GC refers to the place in the new generation of GC, because most of the new generation of Java objects are raw evening toward the dead, so Minor GC very frequently, usually recover relatively fast speed. When Eden is not enough space to allocate memory for the object, triggers Minor GC.

       Old's GC (Full GC / Major GC): Full GC occurs in the GC means the old era, the emergence of Full GC usually accompanied by at least one of the Minor GC (old target's mostly Minor GC into the process from the new generation years old), such as: distribution guarantees failure. Full GC generally slower than the speed of more than 10 times Minor GC. When less than old's memory or explicitly call System.gc () method is recommended JVM recovery, if recovery JVM really triggers a Full GC.

 

Reproduced in https://www.cnblogs.com/fangfuhai/p/7206944.html

Guess you like

Origin www.cnblogs.com/sky-yemeng/p/11728519.html