In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the meaning of permanent generation and metaspace in JVM, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Permanent generation
There is a part of Java's memory called the method area. Before JDK8, the Hotspot virtual machine was implemented as Permanent Generation, which is not available in any other JVM.
In the past (when custom class loaders were not commonly used), classes were almost "static" and rarely unloaded and recycled, so classes could also be seen as "permanent". In addition, because classes are part of the JVM implementation, they are not created by the program because they are also considered "non-heap" memory.
The permanent generation is a continuous memory space, and we can control the size of the permanent generation by setting the value of-XX:MaxPermSize before starting the JVM. The default size of the permanent generation for 32-bit machines is 64m, which is 85m.
The permanent generation of garbage collection and the old era of garbage collection is bound, once one of the areas is occupied, both areas will be garbage collection. But there is an obvious problem, because we can set the size of the permanent generation through-XX:MaxPermSize, once the metadata of the class exceeds the set size, the program will run out of memory and have a memory overflow error (java.lang.OutOfMemoryError: PermGen space).
Why does the metadata of a class take up so much memory? Because in pre-JDK7 HotSpot virtual machines, strings included in the string constant pool are stored in permanent generations, resulting in a series of performance problems and memory overflow errors.
In order to solve these performance problems, and to enable Hotspot to be managed like other virtual machines, metaspace is created.
Meta-space
Meta-space is a new content added by Hotspot in JDK8, and its essence is similar to the permanent generation, which is the realization of the legal area in the JVM specification. But the biggest difference between metaspace and permanent generation is:
The metaspace is not in the virtual machine, but uses local memory. Therefore, by default, the size of metaspace is limited by local memory only, but you can specify the size of metaspace with the following parameters:
-XX:MetaspaceSize
The initial space size, which triggers garbage collection for type unloading, and GC adjusts the value: if a large amount of space is freed, the value is lowered appropriately; if very little space is freed, the value is increased appropriately when the MaxMetaspaceSize is not exceeded. -XX:MaxMetaspaceSize maximum space, which is unlimited by default.
In addition to the above two options that specify the size, there are two GC-related properties:
-XX:MinMetaspaceFreeRatio
After GC, the minimum percentage of Metaspace remaining space capacity, reducing garbage collection caused by space allocation
-XX:MaxMetaspaceFreeRatio
After GC, the percentage of the maximum Metaspace remaining space capacity, reducing garbage collection caused by freeing space
Remove the influence of the permanent generation
Because the metadata of the class is allocated in local memory, the maximum available memory space for meta-space is the available memory space of the system. As a result, we will not encounter memory overflow errors in the presence of permanent generations, nor will leaked data be moved to the swap area. The end user can set a maximum free space for the metaspace, and if it is not set, JVM will automatically increase the capacity of the metaspace based on the metadata size of the class.
Note: the removal of the permanent generation does not mean that the custom classloader leak problem is solved. Therefore, you must also monitor your memory consumption, because in the event of a leak, it will take up a lot of your local memory and may also make swap swapping worse.
Meta-space memory management
The memory management of metaspace is accomplished by metaspace virtual machine.
Previously, we needed different garbage collectors to process the meta-data of the class, but now we only need to execute the C++ code of the metaspace virtual machine. In metaspace, the life cycle of a class and its metadata is the same as its corresponding class loader. In other words, as long as the classloader survives, the metadata of the class it loads is also alive, so it will not be recycled.
To be exact, the storage area of each class loader is called a meta-space, and all the meta-spaces together are what we always call meta-space. When a class loader is marked by the garbage collector as no longer alive, its corresponding metaspace is reclaimed. There are no operations such as relocation and compression in the process of metaspace recovery. However, the metadata in the metaspace is scanned to determine the Java reference.
How exactly is it managed?
The meta-space virtual machine is responsible for the allocation of meta-space, which is in the form of block allocation. The size of the chunk varies depending on the type of classloader. There is a global list of free chunks in the metaspace virtual machine.
When a class loader needs chunks, it gets and maintains its own list of chunks from the global list of chunks.
When a class loader is no longer alive, the chunks it holds will be released and returned to the global chunk list.
The chunks held by the class loader are divided into multiple blocks, each of which stores the meta-information of a unit. The blocks in the chunks are allocated linearly (in the form of pointer collision allocation). Chunks are allocated from memory-mapped areas. These global virtual memory-mapped areas are linked in the form of a linked list, and once a virtual memory-mapped area is cleared, the memory is returned to the operating system.
Running constant pool
The runtime constant pool is part of the method zone in JDK6 and previous versions of JVM, while the implementation of the method area in the HotSpot virtual machine is Permanent Generation. Therefore, the running constant measurement pool is also in the permanent generation.
But JDK7 and later versions of JVM have moved the string constant pool out of the method area, opening up an area in the Heap to hold the runtime pool.
String.intern () is a Native method that returns a reference to the string in the constant pool if the runtime constant pool already contains a string equal to the content of the String object; if not, creates the same string in the constant pool as the String content and returns a reference to the string created in the constant pool.
Existing problems
As mentioned earlier, metaspace virtual machines are allocated in the form of chunks, and the size of chunks is determined by the type of classloader. The class information is not of a fixed size, so it is possible to allocate free chunks that differ from the size of the chunks required by the class, which can lead to fragmentation. The metaspace virtual machine does not support compression operations at present, so fragmentation is the biggest problem at present.
Thank you for reading this article carefully. I hope the article "what is the meaning of permanent generation and metaspace in JVM" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.