Detailed explanation of the new generation, old generation and permanent generation of the heap in the JVM

 The approximate proportion of JVM new generation, old generation and permanent generation:

Cenozoic

  The new generation is mainly used to store new objects. Usually takes up 1/3 of the heap space. In the new generation, a large number of newly created objects are saved, but most of the objects are dying, so MinorGC and garbage collection are frequently performed in the new generation. The new generation is subdivided into three areas: Eden, SurvivorFrom, and ServivorTo. The default ratio of the three areas is 8:1:1.

  •  Eden area: Most of the newly created objects in Java will be allocated in the Eden area (if the object is too large, it will be directly allocated to the old age). When there is not enough memory in the Eden area, MinorGC is triggered (the new generation uses a replication algorithm), and a garbage collection is performed on the new generation.
  • SurvivorFrom area and To area: When the GC starts, objects will only exist in the Eden area and the Survivor area named From. The To area is empty. After a MinorGc, the surviving objects in the Eden area and the SurvivorFrom area will be moved to the SurvivorTo area. , then the Eden area and the SurvivorFrom area will be emptied, and the age of the surviving object will be +1. If the age of the object reaches 15, it will be directly allocated to the old age. After MinorGC is completed, the functions of the SurvivorFrom area and the SurvivorTo area are exchanged. In the next MinorGC, the surviving objects in the SurvivorTo area and the Eden area will be put into the SurvivorFrom area, and the surviving age of the objects will be calculated.

Second, the old age

  The old generation mainly stores memory objects with a long life cycle in the application. The old age is relatively stable, and MajorGC will not be performed frequently. And MinorGC will be performed once before MaiorGC, so that the new object enters the old age and the space is not enough to trigger. When a large enough contiguous space cannot be found to allocate to the newly created larger object, a MajorGC will be triggered in advance for garbage collection to make space.

  In the old generation, MajorGC uses a mark-sweep algorithm: first scan all objects in the old generation, mark the surviving objects, and then recycle the unmarked objects. MajorGC takes a long time. Because it needs to be scanned and recycled. MajorGC will generate memory fragmentation. When there is no memory allocated to the new object in the old age, an OOM (Out of Memory) exception will be thrown.

 

3. Permanent Generation

  Permanent generation refers to the permanent storage area. Mainly store Class and Meta (metadata) information. Classic is put into the permanent area when it is loaded, which is different from the area where the instance is stored. In Java 8, the prefix generation has been removed and replaced by an area called the "metadata area" (metaspace) . The metaspace is similar to the permanent generation, and both are implementations of the methods in the specification in the JVM. But the biggest difference between the metaspace and the permanent generation is that the metaspace is not in the virtual machine, but uses local memory. Therefore, by default, the size of the metaspace is only limited by local memory. The metadata of the class is placed in native memory, and the string pool and static variables of the class are placed in the Java heap. In this way, how many classes of metadata can be loaded is no longer controlled by MaxPermSize, but by the actual available space of the system.

Reasons for using metaspace instead of permanent generation:

  • In order to solve the OOM problem of the permanent generation, metadata and class objects are stored in the permanent generation, which is prone to performance problems and memory overflow.
  • It is difficult to determine the size of the class and method information, so it is difficult to specify the size of the permanent generation, and the size is prone to overflow of the permanent generation, and too large can easily lead to the overflow of the old generation (the heap memory remains unchanged, and one trades off).
  • The permanent generation will bring unnecessary complexity to the GC, and the recycling efficiency is low.
{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324123501&siteId=291194637