什么时候触发 Full GC?

Ark*_*tos 10 java garbage-collection memory-management

按照我的理解:

次要GC

发生在年轻代的 GC 通常被称为 Minor,因为它需要更少的时间来完成,因为 live-set 通常很小(我说的是典型的 java 应用程序,考虑到弱世代假设)和一个数量较少的复制收集器要重新定位和重新映射的对象。

主要GC

发生在老一代中的 GC 通常称为 Major GC,因为它需要更多的时间来完成,因为 live-set 大部分是大的(与年轻代相比)并且它通常会压缩老一代,并且压缩时间随着老年代大小。

不幸的是,GC 日志将老年代收集报告为 Full GC,而仅收集老年代。但是在 Java 内存管理白皮书中,有一个 Full GC 的概念,其中收集整个堆。

A Full GC will be triggered whenever the heap fills up. In such a case the 
young generation is collected first followed by the old generation. If the 
old generation is too full to accept the content of the young generation,
the young generation GC is omitted and the old generation GC is used to 
collect the full heap, either in parallel or serial. Either way the whole 
heap is collected with a stop-the-world event.
Run Code Online (Sandbox Code Playgroud)

如果年轻代填满时总是有一个 Minor GC,如果老一代填满时总是有一个 Major GC,那么所谓的 Full GC 什么时候发生?如果年轻一代和老一代收集器都在尽自己的一份力,堆怎么会变满?

Ark*_*tos 6

当区域大小发生变化时,会发生年轻代和年老代都被收集的 Full GC。

例如,如果我们提到

-Xms1024m -Xmx2048m -XX:PermSize=512m -XX:MaxPermSize=1024m

JVM 最初以 1GB 的堆开始,但为操作系统保留了 2GB 的空间。因此,随着这些区域使用量的增加,基于 VM 的人体工程学,年轻代和年老代会被调整大小,直到它们达到 2GB 的最大保留大小。

同样的事情也适用于 PermSize,每次 PermGen 调整大小时,都会发生完整的 GC。