Java 8 completely removes the Hotspot JVM from the permanent generation and migrates its original data to Java heap or metaspace. In this article, let's summarize the characteristics of metaspace (meta-space). If there is any mistake, please point out, thank you ~
First, Introduction: Why is the permanent generation removed from the hotspot JVM?
See: JEP 122:remove the Permanent Generation
There are two main reasons:
1. Because permanent generation memory is often insufficient or memory leaks occur, it causes annoying java.lang.OutOfMemoryError:PermGen (very common in Java Web development).
2. Removing the permanent generation can facilitate convergence between the Hotspot JVM and the JRockit VM, because JRockit does not have a permanent generation.
This was part of the JRockit and Hotspot convergence effort. JRockit customers do not need to configure the permanent generation (since JRockit does not having a permanent generation) a ND is accustomed to not configuring the permanent generation.
Depending on the reasons above, the permanent generation is eventually removed, the method area is moved to Metaspace, and the string constants are moved to the Java Heap.
Ii. exploration of meta-space
Due to the relatively small number of metaspace, this is mainly based on the Oracle's official Java Virtual Machine specification and several articles in Oracle blog to summarize.
First, what area is metaspace (meta space)? The official explanation is:
In JDK 8, classes metadata are now stored in the native heap and this space is called Metaspace.
That is, JDK 8 begins to place the metadata of the class into the localized heap memory (native heap), which is called Metaspace, and the Chinese name is the meta space.
1. Advantages
What are the benefits of using localized memory? The most direct manifestation is that the java.lang.OutOfMemoryError:PermGen space problem will no longer exist because the default class's metadata allocation is limited only by the local memory size, that is, how much local memory is left. How big can metaspace theoretically be (seemingly the capacity is also related to the operating system's virtual memory?) Not very clear here), which solves the problem of lack of space. However, it is obviously unrealistic to let Metaspace become infinitely large, so we also want to limit the size of Metaspace: Use the-xx:maxmetaspacesize parameter to specify the size of the Metaspace area. The JVM defaults to dynamically set the size of the maxmetaspacesize as needed at run time.
In addition, it has the following advantages (not fully understood, first put it out, from StackOverflow):
Take advantage of Java Language specification property:classes and associated metadata lifetimes match class loader ' s
Linear allocation only
No individual reclamation (except for redefineclasses and class loading failure)
No GC Scan or compaction
No Relocation for Metaspace objects
2. Garbage collection
If the Metaspace occupies a set maximum, then a GC is triggered to collect the dead objects and class loaders. Depending on the features of JDK 8, G1 and CMS will collect the Metaspace area (typically with full GC).
In order to reduce the frequency and time of garbage collection and control the throughput, it is necessary to monitor and tune the Metaspace properly. If there is a frequent full GC in the Metaspace area, it may indicate a memory leak or a metaspace area where the space is too small.
3. New parameters
-xx:metaspacesize is the initial size allocated to the class metadata space in bytes (the initial high water level on the Oracle logical storage, the initial high-water-mark), which is an estimate. The Metaspacesize value is set for the assembly to extend the garbage collection time. After garbage collection, the size of the class metadata space that caused the next garbage collection may become larger.
-xx:maxmetaspacesize is the maximum value assigned to the class metadata space, which triggers the full GC, which is not limited by default, but should depend on the size of the system memory. This value is dynamically changed by the JVM.
After-xx:minmetaspacefreeratio represents a GC, in order to avoid increasing the size of the metadata space, the minimum scale of the idle class metadata capacity is insufficient to cause garbage collection.
-xx:maxmetaspacefreeratio represents a GC, in order to avoid increasing the size of the metadata space, the maximum proportion of the free class metadata capacity is not enough to cause garbage collection.
4. Monitoring and tuning (to be supplemented)
Visualvm,jstat,jstack can monitor the dynamics of the meta-space and will update here later.
Deep Dive Into the JVM (2)-Quest Metaspace