Detailed explanation and relationship between method area, permanent generation and metaspace in JVM?

First we need to review the general memory map of jvm, as follows:

Oh~, I remembered it. It turns out that the method area belongs to the runtime data area of ​​​​JVM, and its function is to store static data such as class information, method information, and constant pool information.

 (Additional note: The red color in the runtime data area means that the method area and heap are shared by threads, and the others are thread-private)

Okay, after recalling the related concepts of the method area, we get to the point:

What is the method area?

In fact, the method area we often talk about is just a concept of the Java virtual machine specification , just like an interface; then someone must implement the interface, so the permanent generation and metaspace play such a role, they are actually both Method area is the implementation of method area in different jdk versions.

What is the permanent generation?

Before jdk7 and jdk7, the method area was called the permanent generation (PermGen)

At this time, the permanent generation is part of the Java Heap and is used to store static data such as class information, method information, and constant pool information.

The Java heap is the memory area in the JVM that stores object instances and arrays. In other words, the permanent generation is a sub-area of ​​the Java heap.

In other words, the static data stored in the permanent generation is separate from the object instances and arrays stored in the heap, and they have different life cycles and allocation methods.

However, the sizes of the permanent generation and the heap affect each other because they both use JVM heap memory, so their sizes are limited by the JVM heap size.

An extension to the string constant pool:

Note: Before jdk7, the string constant pool was also stored in the permanent generation, but in jdk7, it was separated from the permanent generation and stored in the heap space.

 The permanent generation is distributed as follows:

What is metaspace?

In jdk8, the permanent generation was completely removed, and the method area was officially renamed Metaspace;  

The first thing that needs to be confirmed is that their functions have not changed and are still used to store static data such as class information, method information, and constant pool information.

The biggest difference between them is that the permanent generation exists in the Java heap, and its size is limited by the size of the Java virtual machine itself; while the metaspace is a native memory area, which is separate from the JVM memory area,  so it is only available to the local machine. Memory limitations.

So since it is different from the permanent generation, the metaspace has some advantages that the permanent generation does not have, as follows:

  • It does not cause an OutOfMemoryError error because the metaspace can be resized dynamically.

  • Metaspace uses native memory instead of JVM heap memory, which avoids heap memory fragmentation issues.

  • Garbage collection in the metaspace is separated from garbage collection in the heap, which prevents the application from frequently triggering Full GC due to class loading and unloading during operation.

An extension to the string constant pool:

In jdk8, the string constant pool is stored in the heap, and its original permanent generation has been transformed into a metaspace, and is separated from the java heap space and stored in local memory outside the heap.

The metaspace distribution is as follows:

The above is my understanding of these concepts in the method area. If there is anything you need to add, please remember to leave a comment~

Finally, in fact, the sentence that I think everyone needs to understand most in this article is: the permanent generation and metaspace are the implementations of different JDK versions of the method area. Understanding these three concepts and not forgetting to confuse them is a great gain. , everyone is already great~

 

Guess you like

Origin blog.csdn.net/weixin_52394141/article/details/131172249