(1) JVM Memory Model:
In JVM, the heap space is divided into three generations: young generation, old generation, and permanent generation ).
Young Generation: a young generation is used to store the Java objects just allocated by JVM.
Old Generation: objects in the young generation that have not been recycled by garbage collection will be copied to the old generation.
Permanent generation: Permanent generation stores metadata of class and method. Its size depends on the project size, class, and Method Quantity. Generally, it is enough to set it to 30% mb. The setting principle is to reserve yuan.
. The young and old generations are the objects dynamically generated by storage. Permanent tape stores Java class information, including parsed methods, attributes, fields, and so on. Permanent tapes are basically not involved in garbage collection. The garbage collection discussed here mainly targets the young and old generations. For details, see.The young generation is divided into three parts: One Eden zone and two identical primary or zones. The newly created objects are all placed in the Eden area. It is divided into three parts, mainly for objects with short lifecycles to stay in the young generation as much as possible. When no space is available in the Eden area, perform minorgc (Note: GC: garbage collection, garbage collection) to copy the surviving objects to the same vor. Older generations mainly store objects with long lifecycles, such as cache objects.
(2) garbage collection algorithm:
The JVM memory reclaim process is described as follows (can be combined ):
1. Memory Allocation of objects in the Eden area
2. When the Eden area is full and an object is created, the minorgc is triggered because the requested space is not available to recycle garbage in the young (EDEN + 1 small VOR) area.
3. During minorgc, objects that cannot be recycled by Eden are put into an empty volume VOR (Eden will certainly be cleared ), objects that cannot be recycled by GC in another vor will also be placed in the same vor, and always ensure that one VOR is empty.
4. When Step 4 is used, if the replicated VOR is found to be full, these objects will be copied to the old area, or the replicated VOR is not full, but some objects are old enough, it is also placed in the old zone XX: maxtenuringthreshold
5. When the old area is full, perform fullgc.
Note:
- When the memory of the young generation is full, a common GC is triggered, which only recycles the young generation. In case that the young generation is full, the Eden generation is full, and full vor does not cause GC.
- Full GC will be triggered when the old generation is full, and full GC will recycle the young and old generations at the same time.
- Full GC will also be triggered when the permanent generation is full, which will cause the uninstallation of the class and method metadata.
Another problem is, when will outofmemoryexception be thrown? It is not thrown when the memory is null,:
- Jvm98 % time spent in memory collection
- Memory recycled each time is less than 2%
Sorted from:
Http://ldbjakyo.iteye.com/blog/1490242
Http://hx100.blog.51cto.com/44326/587068
Young Generation (new): a young generation to store the Java objects just allocated by JVM.
Tenured: objects in the young generation that have not been recycled by garbage collection will be copied to the old generation.
Permanent generation (Perm): Permanently stores class and method metadata. The size depends on the project size, class, and Method Quantity. Generally, it is enough to set it to MB, the Setting principle is to reserve 30% of the space.