堆空间划分了代:

年轻代(Young Generation)分为 eden 和 Survivor 两个区,Survivor 又分为2个均等的区,S0 和 S1。
首先,新对象都分配到年轻代的 eden 空间,Survivor 刚开始是空的。

当 eden 满了以后,minor gc 就被触发了。

还被引用的对象被移到第一个 survivor 空间,然后把整个 eden 空间都清理掉。

下一次 minor gc 时还是同样的过程,把 eden 中还被引用的对象移到 survivor 空间,然后清除 eden 空间,只是这次是移到第二个 survivor(S1),同时,把上次 minor gc 移到 S0 中的对象也移到 S1,并增加这些对象的年龄,移到 S1 之后,S0 也被清理掉,这时,eden 和 S0 都干净了。

下一次 minor gc 同理,只是这次换为了 S0,eden 和 S1 都干净了。

这个过程不断重复,这样 survivor 中对象的年龄会一直增长,当达到一定程度(例如8),这个对象就从年轻代转移到了老年代。

这样,老年代中的对象就持续增加。

最后就会触发 major gc 对老年代空间进行清理和压缩。

文档地址:
https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
网友评论