7

在 Java 6 中,我们曾经使用以下 GC 配置来防止OutOfMemoryException在多次重新部署我们的应用后使用 Perm Gen:

-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled

我们正在迁移到 Java 7 并希望使用新的 G1 GC,根据我的阅读,它将类从 Java 内存中的 PermGen 移动到本机内存。

是否有一些标志可以卸载未使用的类?

4

2 回答 2

10

The G1 performs the class unloading during a Full GC, so you do not need to specify any parameters to enable this.

You can see for yourself by using the -XX:+TraceClassUnloading argument.

Also, check out this email thread from the HotSpot GC mailing list: Bug in G1GC it performs Full GC when code cache is full resulting in overkill. They discuss class unloading in G1 quite extensively. In summary, you can use -noclassgc if you are seeing issues with class unloading but usually there are no problems with class unloading in G1.

于 2013-09-23T18:46:56.723 回答
1

G1 在 Remark 阶段执行类卸载,即 stop-the-world:

[GC remark 2019-03-26T14:27:52.926+0000: 18.798: [Finalize Marking, 0.0004509 secs] 2019-03-26T14:27:52.926+0000: 18.799: [GC ref-proc, 0.0002791 secs] 2019-03-26T14:27:52.926+0000: 18.799: [Unloading, 0.0058844 secs], 0.0073053 secs]

请注意,Java 8 将 Permgen 替换为 Metaspace,并且 CMS 也进行类卸载(使用开关 CMSClassUnloadingEnabled),因此如果您仍然遇到内存不足错误,它也无济于事。

于 2019-04-25T14:22:30.773 回答