In-depth understanding of java virtual machine---JDK8-abandoned permanent generation (PermGen) ushered in Metaspace (12) JDK8-abandoned permanent generation (PermGen) ushered in Metaspace (Metaspace)

Quote: https://www.cnblogs.com/yulei126/p/6777323.html

JDK8-Abandoned Permanent Generation (PermGen) ushered in Metaspace (Metaspace)

 

1. Background

2. Why Abandon the Permanent Generation (PermGen)

3. In-depth understanding of metaspace (Metaspace)

4. Summary

========Body dividing line======

1. Background

1.1 Where is the Permanent Generation (PermGen)?

According to the hotspot jvm structure is as follows (the virtual machine stack and the local method stack are combined):

The above picture is quoted from the network, but there is a problem: both the method area and the heap heap are memory areas shared by threads.

About method area and permanent generation:

In the HotSpot JVM, the permanent generation discussed this time is the method area in the above figure (called the method area in the JVM specification). The "Java Virtual Machine Specification" only specifies the concept of a method area and its function, and does not specify how to implement it. The permanent generation does not exist on other JVMs.

1.2 Abandonment of JDK8 Permanent Generation

The JDK8 permanent generation changes are as follows:

1. New Generation: Eden+From Survivor+To Survivor

2. Old age: OldGen

3. Permanent generation (implementation of method area): PermGen----->replaced with Metaspace (in local memory)

 

 2. Why Abandon the Permanent Generation (PermGen)

 2.1 Official description

Refer to JEP122: http://openjdk.java.net/jeps/122, the original text is intercepted:

Motivation

This is part of the JRockit and Hotspot convergence effort. JRockit customers do not need to configure the permanent generation (since JRockit does not have a permanent generation) and are accustomed to not configuring the permanent generation.

 That is: removing the permanent generation is an effort to integrate the HotSpot JVM and the JRockit VM, because JRockit does not have a permanent generation and does not need to configure the permanent generation.

 2.2 It is easy to cause problems in actual use

Because the permanent generation memory is often insufficient or memory leaks occur, an exception java.lang.OutOfMemoryError: PermGen is thrown

3. In-depth understanding of metaspace (Metaspace)

3.1 Metaspace memory size

Metaspace is the implementation of method area in HotSpot jvm. The method area is mainly used to store class information, constant pool, method data, method code, etc. The method area is logically part of the heap, but in order to distinguish it from the heap, it is usually called "non-heap".

The essence of the metaspace is similar to that of the permanent generation, and it is the implementation of the method area in the JVM specification. 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. , theoretically depends on the virtual memory size of the 32-bit/64-bit system. It can be seen that it is not unlimited and requires configuration parameters.

3.2 Common configuration parameters

1.MetaspaceSize

The initialized Metaspace size, which controls the threshold for GC to occur in the metaspace. After GC, dynamically increase or decrease MetaspaceSize. By default, this value varies from 12M to 20M depending on the platform. Use the Java  -XX:+PrintFlagsInitial command to view the initialization parameters of the machine

2.MaxMetaspaceSize

Limit the upper limit of Metaspace growth to prevent Metaspace from using local memory indefinitely due to certain circumstances, affecting other programs. The default value of this parameter on this machine is 4294967295B (about 4096MB).

3.MinMetaspaceFreeRatio

After the Metaspace GC is performed, the free space ratio of the current Metaspace will be calculated. If the free space ratio is less than this parameter (that is, the actual non-idle ratio is too large and the memory is not enough), the virtual machine will increase the size of the Metaspace. The default value is 40, which is 40%. Setting this parameter can control the growth rate of Metaspace. Too small value will lead to slow growth of Metaspace, and the use of Metaspace will gradually become saturated, which may affect the loading of subsequent classes. A value that is too large will cause the Metaspace to grow too fast and waste memory.

4.MaxMetasaceFreeRatio

After the Metaspace GC is performed, the free space ratio of the current Metaspace will be calculated. If the free space ratio is greater than this parameter, the virtual machine will release part of the Metaspace space. The default value is 70, which is 70%.

5.MaxMetaspaceExpansion

The maximum magnitude when Metaspace grows. The default value of this parameter on this machine is 5452592B (about 5MB).

6.MinMetaspaceExpansion

The minimum amount by which Metaspace grows. The default value of this parameter on this machine is 340784B (about 330KB).

3.3 Testing and Tracking Metaspace Size

 3.3.1. Test String Constants

copy code
copy code
1 public class StringOomMock {
 2     static String  base = "string";
 3     
 4     public static void main(String[] args) {
 5         List<String> list = new ArrayList<String>();
 6         for (int i=0;i< Integer.MAX_VALUE;i++){
 7             String str = base + base;
 8             base = str;
 9             list.add(str.intern());
10         }
11     }
12 }
copy code
copy code

Select the class in eclipse--"run configuration-->java application--"new parameters are as follows:

 Since the maximum memory is set to 20M, it will soon overflow, as shown below:

 Visible in jdk8:

1. String constants are transferred from the permanent generation to the heap.

2. The persistent generation no longer exists, and the PermSize MaxPermSize parameter has been removed. (Look at the last two lines in the picture)

3.3.2. Testing for metaspace overflow

By definition, we test for metaspace overflow by loading a class with the following code:

 

copy code
copy code
1 package jdk8;
 2
 3 import java.io.File;
 4 import java.lang.management.ClassLoadingMXBean;
 5 import java.lang.management.ManagementFactory;
 6 import java.net.URL;
 7 import java.net.URLClassLoader;
 8 import java.util.ArrayList;
 9 import java.util.List;
10
11 /**
12  *
13  * @ClassName:OOMTest
14 * @Description: Simulate class loading overflow (metaspace oom)
15 * @author diandian.zhang
16 * @date April 27, 2017 at 9:45:40 am
17  */
18 public class OOMTest {  
19     public static void main(String[] args) {  
20         try {  
21 //Prepare url  
22             URL url = new File("D:/58workplace/11study/src/main/java/jdk8").toURI().toURL();  
23             URL[] urls = {url};  
24 //Get JMX interface about type loading  
25             ClassLoadingMXBean loadingBean = ManagementFactory.getClassLoadingMXBean();  
26 // used to cache the class loader  
27             List<ClassLoader> classLoaders = new ArrayList<ClassLoader>();  
28             while (true) {  
29 //Load the type and cache the classloader instance  
30                 ClassLoader classLoader = new URLClassLoader(urls);  
31                 classLoaders.add(classLoader);  
32                 classLoader.loadClass("ClassA");  
33 //Display quantity information (the total number of types loaded, the number of currently valid types, the number of types that have been unloaded)  
34                 System.out.println("total: " + loadingBean.getTotalLoadedClassCount());  
35                 System.out.println("active: " + loadingBean.getLoadedClassCount());  
36                 System.out.println("unloaded: " + loadingBean.getUnloadedClassCount());  
37             }  
38         } catch (Exception e) {  
39 e.printStackTrace ();  
40         }  
41     }  
42 }  
copy code
copy code

 

In order to quickly overflow, set the parameters: -XX:MetaspaceSize=8m -XX:MaxMetaspaceSize=80m, the results are as follows:

 

 The above picture confirms that the class loading (the function of the method area) in our JDK8 is no longer in the permanent generation PerGem, but in the Metaspace. It can be viewed with JVisualVM, which is more intuitive.

 4. Summary

This article explains the origin and nature of Metaspace, common configurations, and monitoring tests. The size of the metaspace changes dynamically, but it is not infinite. It is best to pay attention to the size from time to time, so as not to affect the server memory.

1. Background

2. Why Abandon the Permanent Generation (PermGen)

3. In-depth understanding of metaspace (Metaspace)

4. Summary

========Body dividing line======

1. Background

1.1 Where is the Permanent Generation (PermGen)?

According to the hotspot jvm structure is as follows (the virtual machine stack and the local method stack are combined):

The above picture is quoted from the network, but there is a problem: both the method area and the heap heap are memory areas shared by threads.

About method area and permanent generation:

In the HotSpot JVM, the permanent generation discussed this time is the method area in the above figure (called the method area in the JVM specification). The "Java Virtual Machine Specification" only specifies the concept of a method area and its function, and does not specify how to implement it. The permanent generation does not exist on other JVMs.

1.2 Abandonment of JDK8 Permanent Generation

The JDK8 permanent generation changes are as follows:

1. New Generation: Eden+From Survivor+To Survivor

2. Old age: OldGen

3. Permanent generation (implementation of method area): PermGen----->replaced with Metaspace (in local memory)

 

 2. Why Abandon the Permanent Generation (PermGen)

 2.1 Official description

Refer to JEP122: http://openjdk.java.net/jeps/122, the original text is intercepted:

Motivation

This is part of the JRockit and Hotspot convergence effort. JRockit customers do not need to configure the permanent generation (since JRockit does not have a permanent generation) and are accustomed to not configuring the permanent generation.

 That is: removing the permanent generation is an effort to integrate the HotSpot JVM and the JRockit VM, because JRockit does not have a permanent generation and does not need to configure the permanent generation.

 2.2 It is easy to cause problems in actual use

Because the permanent generation memory is often insufficient or memory leaks occur, an exception java.lang.OutOfMemoryError: PermGen is thrown

3. In-depth understanding of metaspace (Metaspace)

3.1 Metaspace memory size

Metaspace is the implementation of method area in HotSpot jvm. The method area is mainly used to store class information, constant pool, method data, method code, etc. The method area is logically part of the heap, but in order to distinguish it from the heap, it is usually called "non-heap".

The essence of the metaspace is similar to that of the permanent generation, and it is the implementation of the method area in the JVM specification. 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. , theoretically depends on the virtual memory size of the 32-bit/64-bit system. It can be seen that it is not unlimited and requires configuration parameters.

3.2 Common configuration parameters

1.MetaspaceSize

The initialized Metaspace size, which controls the threshold for GC to occur in the metaspace. After GC, dynamically increase or decrease MetaspaceSize. By default, this value varies from 12M to 20M depending on the platform. Use the Java  -XX:+PrintFlagsInitial command to view the initialization parameters of the machine

2.MaxMetaspaceSize

Limit the upper limit of Metaspace growth to prevent Metaspace from using local memory indefinitely due to certain circumstances, affecting other programs. The default value of this parameter on this machine is 4294967295B (about 4096MB).

3.MinMetaspaceFreeRatio

After the Metaspace GC is performed, the free space ratio of the current Metaspace will be calculated. If the free space ratio is less than this parameter (that is, the actual non-idle ratio is too large and the memory is not enough), the virtual machine will increase the size of the Metaspace. The default value is 40, which is 40%. Setting this parameter can control the growth rate of Metaspace. Too small value will lead to slow growth of Metaspace, and the use of Metaspace will gradually become saturated, which may affect the loading of subsequent classes. A value that is too large will cause the Metaspace to grow too fast and waste memory.

4.MaxMetasaceFreeRatio

After the Metaspace GC is performed, the free space ratio of the current Metaspace will be calculated. If the free space ratio is greater than this parameter, the virtual machine will release part of the Metaspace space. The default value is 70, which is 70%.

5.MaxMetaspaceExpansion

The maximum magnitude when Metaspace grows. The default value of this parameter on this machine is 5452592B (about 5MB).

6.MinMetaspaceExpansion

The minimum amount by which Metaspace grows. The default value of this parameter on this machine is 340784B (about 330KB).

3.3 Testing and Tracking Metaspace Size

 3.3.1. Test String Constants

copy code
copy code
1 public class StringOomMock {
 2     static String  base = "string";
 3     
 4     public static void main(String[] args) {
 5         List<String> list = new ArrayList<String>();
 6         for (int i=0;i< Integer.MAX_VALUE;i++){
 7             String str = base + base;
 8             base = str;
 9             list.add(str.intern());
10         }
11     }
12 }
copy code
copy code

Select the class in eclipse--"run configuration-->java application--"new parameters are as follows:

 Since the maximum memory is set to 20M, it will soon overflow, as shown below:

 Visible in jdk8:

1. String constants are transferred from the permanent generation to the heap.

2. The persistent generation no longer exists, and the PermSize MaxPermSize parameter has been removed. (Look at the last two lines in the picture)

3.3.2. Testing for metaspace overflow

By definition, we test for metaspace overflow by loading a class with the following code:

 

copy code
copy code
1 package jdk8;
 2
 3 import java.io.File;
 4 import java.lang.management.ClassLoadingMXBean;
 5 import java.lang.management.ManagementFactory;
 6 import java.net.URL;
 7 import java.net.URLClassLoader;
 8 import java.util.ArrayList;
 9 import java.util.List;
10
11 /**
12  *
13  * @ClassName:OOMTest
14 * @Description: Simulate class loading overflow (metaspace oom)
15 * @author diandian.zhang
16 * @date April 27, 2017 at 9:45:40 am
17  */
18 public class OOMTest {  
19     public static void main(String[] args) {  
20         try {  
21 //Prepare url  
22             URL url = new File("D:/58workplace/11study/src/main/java/jdk8").toURI().toURL();  
23             URL[] urls = {url};  
24 //Get JMX interface about type loading  
25             ClassLoadingMXBean loadingBean = ManagementFactory.getClassLoadingMXBean();  
26 // used to cache the class loader  
27             List<ClassLoader> classLoaders = new ArrayList<ClassLoader>();  
28             while (true) {  
29 //Load the type and cache the classloader instance  
30                 ClassLoader classLoader = new URLClassLoader(urls);  
31                 classLoaders.add(classLoader);  
32                 classLoader.loadClass("ClassA");  
33 //Display quantity information (the total number of types loaded, the number of currently valid types, the number of types that have been unloaded)  
34                 System.out.println("total: " + loadingBean.getTotalLoadedClassCount());  
35                 System.out.println("active: " + loadingBean.getLoadedClassCount());  
36                 System.out.println("unloaded: " + loadingBean.getUnloadedClassCount());  
37             }  
38         } catch (Exception e) {  
39 e.printStackTrace ();  
40         }  
41     }  
42 }  
copy code
copy code

 

In order to quickly overflow, set the parameters: -XX:MetaspaceSize=8m -XX:MaxMetaspaceSize=80m, the results are as follows:

 

 The above picture confirms that the class loading (the function of the method area) in our JDK8 is no longer in the permanent generation PerGem, but in the Metaspace. It can be viewed with JVisualVM, which is more intuitive.

 4. Summary

This article explains the origin and nature of Metaspace, common configurations, and monitoring tests. The size of the metaspace changes dynamically, but it is not infinite. It is best to pay attention to the size from time to time, so as not to affect the server memory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325327426&siteId=291194637