Deep understanding of Java virtual Machines---jdk8-obsolete permanent generation (PermGen) usher in Meta Space (Metaspace) (12)

Source: Internet
Author: User
Tags server memory

1. Background

2. Why abandoned permanent generation (PermGen)

3. In-depth understanding of meta-space (Metaspace)

4. Summary

======== Text Split Line =====

First, the background 1.1 permanent generation (PermGen) where?

According to this, the hotspot JVM structure is as follows (the virtual machine stack and the local method stack are up):

Quoted from the network, but there is a problem: both the method area and the heap heap are thread-shared memory areas.

About method areas and permanent generations:

In the Hotspot JVM, the permanent generation of this discussion is the method area (called the method area in the JVM specification). The Java Virtual Machine specification only prescribes the concept of a method area and its role, and does not specify how to implement it. There are no permanent generations on other JVMs.

1.2 JDK8 permanent generation of waste

JDK8 permanent changes such as:

1. Cenozoic: Eden+from survivor+to Survivor

2. The old age: Oldgen

3. Permanent generation (Implementation of the method area): PermGen-----> Replace with metaspace (Local in-memory)

Ii. why abandoned permanent generation (PermGen) 2.1 Official notes

Referring to jep122:http://openjdk.java.net/jeps/122, the original interception:

Motivation

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.

That is, removing the permanent generation is an effort to fuse the hotspot JVM with the JRockit VM, because JRockit does not have a permanent generation and does not need to configure a permanent generation.

2.2 Easy-to-use problems in reality

Abnormal java.lang.OutOfMemoryError:PermGen due to persistent memory is often insufficient or memory leaks occur

Third, in-depth understanding of meta-space (metaspace) 3.1-dimensional memory size

Meta-space is the implementation of the method area in the Hotspot JVM, the method area is mainly used to store the information of the class, Chang, method data, method code and so on. The method area is logically part of the heap, but is often called non-heap in order to differentiate it from the heap.

The nature of meta-space is similar to permanent generation, which is the implementation of the method area in the JVM specification. However , the biggest difference between meta-space and permanent generation is that the meta-space is not in the virtual machine, but rather uses local memory. , which in theory depends on the virtual memory size of the 32-bit/64-bit system. Visible is not unrestricted, configuration parameters are required.

3.2 Common Configuration parameters

1.MetaspaceSize

Initializes the metaspace size, which is the threshold value of the GC for the control meta space. After GC, Metaspacesize is dynamically increased or decreased. By default, this value is sized to float from 12M to 20M depending on the platform. To view native initialization parameters using the java-xx:+printflagsinitial command

2.MaxMetaspaceSize

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

3.MinMetaspaceFreeRatio

When the Metaspace GC is performed, the current metaspace's free space ratio is calculated, and if the idle ratio is less than this parameter (that is, the actual non-idle account is too large and the memory is not enough), the virtual machine will grow metaspace size. The default value is 40, which is 40%. Setting this parameter can control the speed at which the metaspace grows, too small a value can cause metaspace to grow slowly, and the use of metaspace gradually becomes saturated, which may affect the loading of classes later. Too large a value can cause metaspace to grow too fast and waste memory.

4.MaxMetasaceFreeRatio

When the Metaspace GC is performed, the current metaspace's free space ratio is calculated, and if the idle ratio is greater than this parameter, then the virtual opportunity frees up part of the metaspace space. The default value is 70, which is 70%.

5.MaxMetaspaceExpansion

The maximum amplitude of metaspace growth. The default value for this parameter on this computer is 5452592B (approximately 5MB).

6.MinMetaspaceExpansion

The minimum amplitude of metaspace growth. The default value for this parameter on this computer is 340784B (approximately 330KB).

3.3 Test and trace the meta-space size 3.3.1. Test string Constants
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.int Ern ());         }11     }12}

Select class in Eclipse-"Run Configuration-->java application--" The new parameter is as follows:

Since the maximum memory 20M is set, it will quickly overflow, such as:

Visible in the JDK8:

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

2. The persistent generation does not exist and the PermSize maxpermsize parameter has been removed. (see the last two lines in the picture)

3.3.2. Test meta-space overflow

By definition, we test the meta-space overflow with the load class, with the following 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/**12 * @ClassName: OOMTest14 * @Description: Simulation class load overflow (meta space oom) * @author Diandian.zh         ANG16 * @date April 27, 2017 morning 9:45:4017 */18 public class Oomtest {20-public static void main (string[] args) try {21//Prepare URL at URL url = new File ("D:/58workplace/11study/src/main/java/jdk8"). Touri ()  . Tourl ();  url[] urls = {URL}; 24//Get the JMX interface for type loading Classloadingmxbean Loadingbean = Managementfactory.getclassloadingmxbean ()  ;  26//For Cache class Loader list<classloader> classloaders = new arraylist<classloader> (); (true) {29//load type and cache class Loader instance ClassLoader ClassLoader = new URLClassLoader (URLs);  Classloaders.add (ClassLoader);  Classloader.loadclass ("ClassA"); 33//Display quantity information (number of types that have been loaded, number of types currently in effect, number of types that have been unloaded) System.out.println ("total:" + Loadingbea  N.gettotalloadedclasscount ());  SYSTEM.OUT.PRINTLN ("active:" + loadingbean.getloadedclasscount ());  System.out.println ("unloaded:" + loadingbean.getunloadedclasscount ());  PNS} catch (Exception e) {e.printstacktrace ();   40} 41} 42}

In order to quickly overflow, set the parameters:-xx:metaspacesize=8m-xx:maxmetaspacesize=80m, run the result as follows:

It is confirmed that the class loading in our JDK8 (the function of the method area) is not in the permanent generation Pergem, but in the metaspace. Can be combined with JVISUALVM to see, more intuitive.

Iv. Summary

This article explains the origin and nature of meta-space (metaspace), common configurations, and monitoring tests. The size of the meta-space is dynamically changed, but not infinitely large, it is best to always focus on the size, so as not to affect server memory.

Deep understanding of Java virtual Machines---jdk8-obsolete permanent generation (PermGen) usher in Meta Space (Metaspace) (12)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.