Java PermGen 和 Metaspace 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27131165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between PermGen and Metaspace?
提问by Kao
Until Java 7 there was an area in JVM memory called PermGen, where JVM used to keep its classes. In Java 8it was removed and replaced by area called Metaspace.
在 Java 7 之前,JVM 内存中有一个称为PermGen的区域,JVM 曾经在其中保存其类。在Java 8 中它被移除并被称为Metaspace的区域取代。
What are the most important differencesbetween PermGen and Metaspace?
什么是最重要的区别PermGen的和元空间之间?
The only difference I know is that java.lang.OutOfMemoryError: PermGen space
can no longer be thrown and the VM parameter MaxPermSize
is ignored.
我知道的唯一区别是java.lang.OutOfMemoryError: PermGen space
不能再抛出并且MaxPermSize
忽略VM 参数。
采纳答案by Mattias Jiderhamn
The main difference from a user perspective - which I think the previous answer does not stress enough - is that Metaspace by default auto increasesits size (up to what the underlying OS provides), while PermGen always has a fixed maximum size. You can set a fixed maximum for Metaspace with JVM parameters, but you cannot make PermGen auto-increase.
从用户角度来看,主要区别——我认为之前的答案不够强调——默认情况下 Metaspace 会自动增加其大小(取决于底层操作系统提供的大小),而 PermGen 始终具有固定的最大大小。您可以使用 JVM 参数为 Metaspace 设置固定的最大值,但不能使 PermGen 自动增加。
To a large degree it is just a change of name. Back when PermGen was introduced, there was no Java EE or dynamic class(un)loading, so once a class was loaded it was stuck in memory until the JVM shut down - thus PermanentGeneration. Nowadays classes may be loaded and unloaded during the lifespan of the JVM, so Metaspace makes more sense for the area where the metadata is kept.
在很大程度上,这只是更名而已。当 PermGen 被引入时,没有 Java EE 或动态类(卸载)加载,所以一旦一个类被加载,它就会被卡在内存中,直到 JVM 关闭——因此是永久代。如今,类可能会在 JVM 的生命周期内加载和卸载,因此元空间对于保存元数据的区域更有意义。
Both of them contain the java.lang.Class
instances and both of them suffer from ClassLoader leaks. Only difference is that with Metaspace default settings, it takes longer until you notice the symptoms (since it auto increases as much as it can), i.e. you just push the problem further away without solving it. OTOH I imagine the effect of running out of OS memory can be more severe than just running out of JVM PermGen, so I'm not sure it is much of an improvement.
它们都包含java.lang.Class
实例,并且都存在ClassLoader 泄漏。唯一的区别是,使用 Metaspace 默认设置,需要更长的时间才能注意到症状(因为它会尽可能多地自动增加),即您只是将问题推得更远而不解决它。OTOH 我想 OS 内存耗尽的影响可能比 JVM PermGen 耗尽更严重,所以我不确定这是否有很大的改进。
Whether you're using a JVM with PermGen or with Metaspace, if you are doing dynamic class unloading, you should to take measures against classloader leaks, for example by using my ClassLoader Leak Prevention library.
无论您是使用 PermGen 还是 Metaspace 的 JVM,如果您正在进行动态类卸载,您都应该采取措施防止类加载器泄漏,例如使用我的ClassLoader Leak Prevention 库。
回答by Ankur Singhal
Bye, Bye PermGen, Hello Metaspace
再见,再见永久代,你好元空间
PermGenhas been completely removed.
PermGen已被完全删除。
Metaspace garbage collection- Garbage collection of the dead classes and classloaders is triggered once the class metadata usage reaches the MaxMetaspaceSize
.
元空间垃圾收集- 一旦类元数据使用达到MaxMetaspaceSize
.
The space Metadata
was held is no longer contiguous to the Java heap
, The metadata
has now moved to native memory to an area known as the Metaspace
.
空间Metadata
举行不再是连续的Java heap
,在metadata
现在已经转移到本机内存,以被称为一个区域Metaspace
。
In Simple words,
简单来说,
Since the class metadata is allocated out of native memory, the max available space is the total available system memory. Thus, you will no longer encounter OOM errors
and could end up spilling into the swap space.
由于类元数据是在本机内存之外分配的,因此最大可用空间是总可用系统内存。因此,您将不再遇到OOM errors
并可能最终溢出到交换空间。
The removal of PermGen
doesn't mean that your class loader leak issues are gone. So, yes, you will still have to monitor your consumption and plan accordingly, since a leak would end up consuming your entire native memory.
删除PermGen
并不意味着您的类加载器泄漏问题消失了。所以,是的,您仍然需要监控您的消耗并相应地进行计划,因为泄漏最终会消耗您的整个本机内存。
回答by Chandrasekaran
In short, Metaspace size auto increases in native memory as required to load class metadata if not restricted with -XX:MaxMetaspaceSize
简而言之,如果不受限制,元空间大小会根据加载类元数据的需要在本机内存中自动增加 -XX:MaxMetaspaceSize