JVM 标志 CMSClassUnloadingEnabled 实际上是做什么的?

2022-01-16 00:00:00 jvm java classloader jvm-arguments

除了一些非常模糊的高级定义,例如摆脱你的 PermGen 问题"(它没有,顺便说一句).

I cannot for the life of me find a definition of what the Java VM flag CMSClassUnloadingEnabled actually does, other than some very fuzzy high-level definitions such as "gets rid of your PermGen problems" (which it doesn't, btw).

我查看了 Sun/Oracle 的网站,甚至查看了 选项列表 实际上并没有说明它的作用.

I have looked on Sun's/Oracle's site, and even the options list doesn't actually say what it does.

根据标志的名称,我猜测 CMS 垃圾收集器默认情况下不会卸载类,并且此标志将其打开 - 但我不能确定.

Based upon the name of the flag, I'm guessing that the CMS Garbage Collector doesn't by default unload classes, and this flag turns it on - but I can't be sure.

推荐答案

更新 此答案与 Java 5-7 相关,Java 8 已修复此问题:https://blogs.oracle.com/poonam/about-g1-garbage-collector,-permanent-generation-and-metaspace 感谢 mt.uulu

Update This answer is relevant for Java 5-7, Java 8 has this fixed: https://blogs.oracle.com/poonam/about-g1-garbage-collector,-permanent-generation-and-metaspace Kudos go to mt.uulu

对于 Java 5-7:

For Java 5-7:

Oracle/Sun VM 对世界的标准看法是:类是永恒的.所以一旦加载,即使没有人关心,它们也会留在内存中.这通常没有问题,因为您没有那么多纯粹的设置"类(= 一次用于设置,然后不再使用).所以即使它们占用 1MB,谁在乎呢.

The standard Oracle/Sun VM look on the world is: Classes are forever. So once loaded, they stay in memory even if no one cares anymore. This usually is no problem since you don't have that many purely "setup" classes (= used once for setup and then never again). So even if they take up 1MB, who cares.

但最近,我们有了像 Groovy 这样的语言,它们在运行时定义类.每次运行脚本时,都会创建一个(或多个)新类,并且它们会永远保留在 PermGen 中.如果您正在运行服务器,则意味着您有内存泄漏.

But lately, we have languages like Groovy, that define classes at runtime. Every time you run a script, one (or more) new classes are created and they stay in PermGen forever. If you're running a server, that means you have a memory leak.

如果启用 CMSClassUnloadingEnabled,GC 也会扫描 PermGen,并删除不再使用的类.

If you enable CMSClassUnloadingEnabled the GC will sweep PermGen, too, and remove classes which are no longer used.

您还必须启用 UseConcMarkSweepGC(感谢 山姆·哈斯勒).看到这个答案:https://stackoverflow.com/a/372​​0052/2541

You will also have to enable UseConcMarkSweepGC (thanks to Sam Hasler). See this answer: https://stackoverflow.com/a/3720052/2541

相关文章