The translator MeiXiaoXi, the original reference: http://blog.csdn.net/wang8118/article/details/45765869

Java 8 introduces some new languages and runtime features. One of these features is the complete removal of PermGen, a decision Oracle has announced since the release of JDK1.7. Internal strings, for example, have been removed from the persistent generation since JDK1.7, and the release of JDK8 abolished them completely. In this section, we will discuss the successor to persistent generation: Metaspace.

We will compare the runtime behavior of HotSpot 1.7 and HotSpot 1.8 when executing a Java program with a “leak” -like metadata object.

Metaspace: A new memory space is born

The JDK8 HotSpot JVM now uses native memory to store class metadata, called Metaspace, similar to Oracle JRockit and IBM JVMS.

The good news is that it means that the Java. Lang. OutOfMemoryError: PermGen space problem will be less and less, also no longer need you to adjust and monitor memory space. However, this change is not visible by default, and we will show you that you still need to pay attention to the class metadata memory footprint. Keep in mind that these new features won’t magically eliminate memory leaks from classes and classloaders. You need to use different methods and learn new naming conventions to get to the root of the problem.

Conclusion:

• This memory area is completely removed. • The PermSize and MaxPermSize JVM parameters are ignored and are warned at startup.

• Most memory allocations for class metadata do not occur in local memory. • Class objects used to describe class metadata are removed.

Metaspace capacity • By default, class metadata allocation is limited to available local memory (the capacity depends on the virtual memory available to your operating system using either a 32-bit JVM or a 64-bit JVM). • A new tag is available (MaxMetaspaceSize) that allows you to limit the amount of local memory used for class metadata. If you do not specify this tag, Metaspace dynamically controls the size based on the needs of the runtime application.

Metaspace garbage collection • Once the usage of class metadata reaches the value specified by “MaxMetaspaceSize”, garbage collection is triggered for useless classes and classloaders. • Appropriate monitoring and tuning of Metaspace is necessary to control the frequency and latency of this garbage collection. Excessive Metaspace garbage collection is a sign that your classes and classloaders are leaking memory, as well as that your application’s memory size is not appropriate and needs to be adjusted.

Java heap space impact • Some miscellaneous data is moved to the Java heap space. This means that you will see the Java heap space grow when you update to JDK8.

Metaspace monitoring • Metaspace usage can be observed through HotSpot 1.8’s verbose GC log output. • Jstat and JVisualVM have not been updated at the time of testing on B75, and the old persistent generation space references still exist.

Enough theory here, let’s take a look at the new memory space in action via leaked Java programs…

Persistent generation vs. Metaspace runtime comparison

To better understand the behavior of the new Metaspace memory space at runtime, we created a Java program that was exposed to metadata leaks. You can download it here.

The following scenario will be tested: • Running this Java program using JDK1.7 in order to monitor and consume the 128M persistent generation space set up. • Run this Java program using JDK1.8(B75) to monitor dynamic growth of Metaspace memory space and garbage collection. • Run the Java program using JDK1.8(B75) and set MaxMetaspaceSize to 128M to simulate Metaspace space consumption.

JDK 1.7@64-bit — Persistent generation consumption • a program with 50,000 configured iterations • 1024M Java heap • 128M Java persistent generation (-xx :MaxPermSize= 128M)

As you can see from JVisualVM, persistent generation consumption almost reaches critical mass after loading over 30,000 classes. We can also see this consumption in Java programs and GC output.

 

Now let’s use HotSpot JDK 1.8 to execute the program.

JDK 1.8@64-bit — Metaspace dynamic size • a program with 50,000 configured iterations • 1024M heap • Java Metaspace space: unlimited (default)

 

As you can see from the detailed GC output, the JVM’s Metaspace does dynamically expand local memory from 20M to 320M in order to accommodate the growing memory footprint of class metadata in Java programs. We can also observe events where the JVM will attempt garbage collection in order to eliminate useless classes and classloader objects. Since our Java programs started leaking memory, the JVM had no choice but to dynamically expand the Metaspace memory space.

The program can run 50,000 iterations without an OOM event and has over 50,000 classes loaded.

Let’s move on to our final test scenario:

JDK 1.8@64-bit — Metaspace consumption • a program with 50,000 configured iterations • 1024M heap • Java Metaspace space: 128 MB (-xx :MaxMetaspaceSize= 128M)

As you can see from JVisualVM, after loading over 30,000 classes, Metaspace consumption reaches critical, similar to the result when running with JDK1.7. We can see this result in the program and in the GC output. Another interesting observation is that the local memory footprint is twice the specified maximum. This might suggest that a good strategy for adjusting metaspace capacity might avoid wasting local memory.

As with JDK1.7, we specified a maximum capacity of 128M for metaspace, but it does not complete 50,000 iterations in our program. New OOM will be thrown. The OOM event above was thrown by the JVM from Metaspace after memory allocation failed. .

Summary of metaspace

The results observed so far show that proper monitoring and tuning is necessary to avoid problems like the excessive Metaspace GC or OOM triggers in our last test scenario.

There is no shortage of job opportunities for good people, only good opportunities for themselves. But they often don’t have the energy to sift through a sea of opportunities to find the best fit. 100Offer will strictly screen talents and enterprises on the platform, so that “the best talents” and “the best companies” meet. Scan the QR code below to register 100Offer and talk about your expectations for your next job. Within a week, you’ll receive 5-10 great opportunities to meet your needs!