foreword
This article systematically explains the method area in the JVM thread shared memory of java.
1. Concept of method area & metaspace
The method area is a memory area specified in the "Java Virtual Machine Specification", which is used to store type information, constants, static variables, code caches compiled by the just-in-time compiler, etc. that have been loaded by the virtual machine.
The metaspace is the implementation of the method area. The implementation of the method area, before JDK1.7 is the permanent generation, after JDK1.8 is the metaspace.
2. The difference with the permanent generation
Differences between permanent generation and metaspace:
1. The permanent generation is physically part of the heap, and the metaspace memory is the local memory of the operating system.
2. Put StringTable in the heap space in jdk7. Because the recycling efficiency of the permanent generation is very low, it will only be triggered during full gc. The full gc is only triggered when there is insufficient space in the old generation and the permanent generation is insufficient. This leads to inefficient recycling of StringTable. In our development, a large number of strings will be created, and the recycling efficiency is low, resulting in insufficient memory in the permanent generation. Putting it in the heap can reclaim memory in time.
3. Recycling of Metaspace
The size of the metaspace will be continuously adjusted after the GC during the running of the JAVA program:
(Related parameters can be found later in the article)
4. CompressedClassSpace
4.1. What is class compression space
On 64-bit platforms, HotSpot uses two compression optimization techniques, Compressed Object Pointers ("CompressedOops") and Compressed Class Pointers.
Compressed pointer refers to a way to use 32-bit pointers to access data (objects in the heap or metadata in Metaspace) on a 64-bit machine.
4.2. Why there is class compression space
32-bit pointers take up less memory and allow for better cache usage
4.3. Class compression space opening and parameters
The parameters involved are as follows:
-XX:+UseCompressedOops Enable object pointer compression. Enabled by default.
-XX:+UseCompressedClassPointers Enable class pointer compression. Enabled by default.
-XX:CompressedClassSpaceSize Class compressed space size, the default is 1G, and cannot be greater than 3G.
Requirements for Class Compressed Space to be Enabled
1. The enablement of class pointer compression must require the enablement of object pointer compression, and vice versa
2. Object pointer compression requires the heap size to be less than 32G, so the class compression space requires the heap size to be less than 32G
4.4 Introduction to the Principle of Class Compression
Note:
A. What is compressed is the pointer that refers to klass, not klass.
B. If the class compression space is not enabled, then the klass data is stored in the non-klass space.
klass and non-klass spaces
A class is loaded and the memory allocation is as follows:
5. Parameter introduction and optimization suggestions
Note: You can use java -XX:+PrintFlagsFinal -version |grep [parameter] to view the default value of the parameter
a、MetaspaceSize
The initial space size, reaching this value will trigger garbage collection for type unloading, and the GC will adjust this value: if a large amount of space is released, this value should be appropriately reduced; if very little space is released, this value should be appropriately increased when it does not exceed MaxMetaspaceSize.
Personal suggestions can be set to the same size as MaxMetaspaceSize.
b、MaxMetaspaceSize
The maximum metaspace size, the default is no limit, the maximum limit is the limit of the local memory of the operating system
suggestion:
1. It is best to limit the maximum size of the metaspace, so as to avoid excessive memory usage of the operating system, and avoid late exposure of code problems (such as leaks in class loaders).
2. If you find that the GC of the application metaspace is too frequent after startup, you should increase it.
c、MinMetaspaceFreeRatio
Minimum idle ratio. When Metaspace GC occurs, the idle ratio of Metaspace will be calculated. If the idle ratio (free space/current Metaspace size) is less than this value, Metaspace expansion will be triggered. The default value is 40.
d、MaxMetaspaceFreeRatio
The maximum idle ratio. When the Metaspace GC occurs, the idle ratio of the Metaspace will be calculated. If the idle ratio (free space/current Metaspace size) is greater than this value, the Metaspace will be triggered to release space. The default value is 70.
e、MinMetaspaceExpansion和MaxMetaspaceExpansion
These two do not directly limit the size of metaspace expansion, but to increase the threshold for triggering metaspace GC.
MinMetaspaceExpansion defaults to 332.8K, increasing the minimum requirement to trigger the threshold of metaspace GC.
If the memory to be allocated is less than MinMetaspaceExpansion, increase the threshold of metaspace GC to MinMetaspaceExpansion.
MaxMetaspaceExpansion defaults to 5.2M, increasing the maximum requirement to trigger the threshold of metaspace GC.
If the memory to be allocated is greater than MinMetaspaceExpansion but less than MaxMetaspaceExpansion, the increment is MaxMetaspaceExpansion.
If the memory to be allocated exceeds MaxMetaspaceExpansion, the increment is MinMetaspaceExpansion plus the memory size to be allocated
Note: Each allocation will only give the corresponding thread a chance to expand the triggering metaspace GC threshold. If the expansion is not possible, but the allocation is still not possible, then you can only wait for GC.
f、CompressedClassSpaceSize
Class compression space size, that is, the size of klass space, the default is 1G, preferably not more than 3G.
6. Common errors
6.1、java.lang.OutOfMemoryError: Metaspace
When this error occurs, it is obvious that too many classes are loaded, and the memory of the metaspace is used up.
The first step should be to check whether your own -XX:MaxMetaspaceSize setting is appropriate, you can increase it appropriately, and then observe whether the application will still appear. This is a palliative, not a permanent cure, but it is the fastest effective.
But if it is found that the problem still exists, and the value of -XX:MaxMetaspaceSize can no longer be set to a larger value, further investigation is required. The investigation is mainly carried out in the following two directions:
A. The referenced jar package loads a lot of classes
Check the large jar package introduced by the application, remove redundant add-ons, and load them at the smallest granularity.
B. Too many dynamically generated classes
Some Java frameworks use dynamic proxy technology to dynamically generate classes (such as spring). Creating objects in this way may create a large number of classes, causing the metaspace to be full.
6.2、java.lang.OutOfMemoryError: Compressed class space
The metaspace itself does not need to set MaxMetaspaceSize, so that there is no limit to the size of the metaspace, only limited by the size of the local memory.
But class compression space is limited, even if CompressedClassSpaceSize is not set, there will still be a limit of 1G.
The analysis direction is the same as java.lang.OutOfMemoryError: Metaspace.
7. Metaspace analysis method
Here we only talk about the corresponding tools or commands to analyze the knowledge points, specific commands and tools will be explained separately in subsequent articles
7.1, jinfo command
View parameter configuration
jinfo -flag MetaspaceSize 【PID】
7.2. jstat command
View Metaspace Usage Percentage
jstat -gcutil 344537 | column -t
View the current total size and usage size of metaspace
jstat -gc 【PID】| column -t
MC is the current metaspace size
MU is the current metaspace usage size
CCSC is the compressed class space size
CCSU is the compressed class space usage size
Metaspace Statistics
jstat -gcmetacapacity 【PID】| column -t
MCMN: minimum metadata capacity
MCMX: maximum metadata capacity
MC: current metadata space size
CCSMN: Minimum Compressed Class Space Size
CCSMX: maximum compressed class space size
CCSC: current compressed class space size
YGC: Number of young generation garbage collections
FGC: the number of garbage collections in the old age
FGCT: Old generation garbage collection time consumption
GCT: Garbage Collection Consumed Total Time
7.3, arthas tools
View the loading status of each class loader
classloader
numberOfInstances: the number of instances of classloader
loadedCountTotal: the number of classes loaded by this instance
View statistics by class loading instance, you can see the hashcode of class loading
classloader -l
View the inheritance relationship of class loading
classloader -t
View the actual location of the class loader