Permgen vs Metaspace in Java – Permgen公司与Metaspace公司在Java方面的竞争

最后修改: 2018年 9月 9日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this quick tutorial, we’re going to examine the differences between the PermGen and Metaspace memory regions in the Java environment.

在这个快速教程中,我们将研究Java环境中PermGen和Metaspace内存区域的区别

It’s important to keep in mind that, starting with Java 8, the Metaspace replaces the PermGen – bringing some substantial changes.

重要的是要记住,从Java 8开始,Metaspace取代了PermGen–带来一些实质性的变化。

2. PermGen

2.PermGen

PermGen (Permanent Generation) is a special heap space separated from the main memory heap.

PermGen(永久生成)是一个与主内存堆分离的特殊堆空间

The JVM keeps track of loaded class metadata in the PermGen. Additionally, the JVM stores all the static content in this memory section. This includes all the static methods, primitive variables, and references to the static objects.

JVM在PermGen中保持对加载的类元数据的跟踪。 此外,JVM在这个内存部分存储所有的静态内容。这包括所有的静态方法、原始变量,以及对静态对象的引用。

Furthermore, it contains data about bytecode, names, and JIT information. Before Java 7, the String Pool was also part of this memory. The disadvantages of the fixed pool size are listed in our write-up.

此外,它包含关于字节码、名称和JIT信息的数据。在 Java 7 之前,字符串池也是该内存的一部分。固定池大小的缺点在我们的写作中列出。

The default maximum memory size for 32-bit JVM is 64 MB and 82 MB for the 64-bit version.

32位JVM的默认最大内存大小为64MB,64位版本为82MB。

However, we can change the default size with the JVM options:

然而,我们可以通过JVM选项改变默认大小。

  • -XX:PermSize=[size] is the initial or minimum size of the PermGen space
  • -XX:MaxPermSize=[size] is the maximum size

Most importantly, Oracle completely removed this memory space in the JDK 8 release. Therefore, if we use these tuning flags in Java 8 and newer versions, we’ll get the following warnings:

最重要的是,Oracle在JDK 8版本中完全删除了这个内存空间。因此,如果我们在Java 8和更新的版本中使用这些调整标志,我们会得到以下警告。

>> java -XX:PermSize=100m -XX:MaxPermSize=200m -version
OpenJDK 64-Bit Server VM warning: Ignoring option PermSize; support was removed in 8.0
OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
...

With its limited memory size, PermGen is involved in generating the famous OutOfMemoryError. Simply put, the class loaders weren’t garbage collected properly and, as a result, generated a memory leak.

由于其有限的内存大小,PermGen参与产生了著名的OutOfMemoryError。简单地说,class loaders没有正确地进行垃圾收集,因此产生了内存泄漏。

Therefore, we receive a memory space error; this happens mostly in the development environment while creating new class loaders.

因此,我们会收到内存空间错误;这主要发生在开发环境中创建新类加载器时。

3. Metaspace

3.元空间

Simply put, Metaspace is a new memory space – starting from the Java 8 version; it has replaced the older PermGen memory space. The most significant difference is how it handles memory allocation.

简单地说,Metaspace是一个新的内存空间–从Java 8版本开始;它已经取代了旧的PermGen内存空间。最显著的区别是它如何处理内存分配。

Specifically, this native memory region grows automatically by default.

具体来说,这个本地内存区域默认会自动增长

We also have new flags to tune the memory:

我们也有新的标志来调整记忆。

  • MetaspaceSize and MaxMetaspaceSize – we can set the Metaspace upper bounds.
  • MinMetaspaceFreeRatio – is the minimum percentage of class metadata capacity free after garbage collection
  • MaxMetaspaceFreeRatio – is the maximum percentage of class metadata capacity free after a garbage collection to avoid a reduction in the amount of space

Additionally, the garbage collection process also gains some benefits from this change. The garbage collector now automatically triggers the cleaning of the dead classes once the class metadata usage reaches its maximum metaspace size.

此外,垃圾收集过程也从这个变化中获得了一些好处。一旦类的元数据使用量达到其最大的元空间大小,垃圾收集器现在会自动触发对死类的清理。

Therefore, with this improvement, JVM reduces the chance to get the OutOfMemory error.

因此,通过这一改进,JVM减少了获得OutOfMemory错误的机会

Despite all of these improvements, we still need to monitor and tune the metaspace to avoid memory leaks.

尽管有所有这些改进,我们仍然需要监控和调整元空间以避免内存泄漏。

4. Summary

4.总结

In this quick write-up, we presented a brief description of PermGen and Metaspace memory regions. Additionally, we explained the key differences between each of them.

在这篇快速撰写的文章中,我们介绍了PermGen和Metaspace内存区域的简要描述。此外,我们还解释了它们之间的主要区别。

PermGen is still around with JDK 7 and older versions, but Metaspace offers more flexible and reliable memory usage for our applications.

PermGen仍然存在于JDK 7和旧版本中,但Metaspace为我们的应用程序提供了更灵活和可靠的内存使用。