为什么JVM的GC喜欢不变的对象? [英] Why are immutable objecs loved by JVM's GC?

查看:112
本文介绍了为什么JVM的GC喜欢不变的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道JVM GC喜欢短生命对象的原因,因为它可以在小GC中收集。但为什么JVM GC爱不可变的对象?

编辑:
Charlie Hunt说GC在他的 presentation



谢谢 p>如果GC可以知道某个对象不包含任何对任何gen0对象的引用,那么在执行gen0集合时可以忽略它。同样,如果某个对象不包含对任何gen0或gen1对象的引用,则在执行gen1集合时可能会忽略它。在收集过程中可以忽略的对象越多,收集的速度就越快。



如果一个对象在gen0 GC中存在,可以确定任何gen0对象它曾举办过一次参考,将被提升到gen1;同样,如果一个不包含任何gen0引用的对象存在于gen1 GC中,它包含的任何gen1引用将被提升为gen2。因此,一旦在一个gen0集合中检查了一个对象,直到下一个gen1集合,除非它被修改,否则不需要再次检查它。同样,在gen1集合中检查的对象不需要检查直到下一个gen2集合被修改



知道对象是否已被修改是一个棘手的问题,但关键是如果对象没有被发现,它对于GC来说是非常有利的。


I know the reason that JVM GC loves short-live object because it can be collected in minor GC. But why does JVM GC love immutable objects?

EDIT: Charlie Hunt says that GC loves immutable objects in his presentation.

Thanks

解决方案

If the GC can know that an object doesn't contain any references to any gen0 objects, then it can be ignored when performing a gen0 collection. Likewise if an object doesn't contain any reference to any gen0 or gen1 objects, it may be ignored when performing a gen1 collection. The more objects can be ignored during a collection, the faster that collection will be.

If an object survives a gen0 GC, it can be sure that any gen0 object to which it had held a reference will have been promoted to gen1; likewise if an object which didn't contain any gen0 references survives a gen1 GC, any gen1 references it contained will have been promoted to gen2. Thus, once a object has been examined during a gen0 collection, it need not be examined again until the next gen1 collection, unless it is modified. Likewise an object that's examined during a gen1 collection need not be examined until the next gen2 collection unless it is modified.

Knowing whether objects have been modified is a tricky subject, but the key point is that it's very advantageous for the GC if objects haven't been.

这篇关于为什么JVM的GC喜欢不变的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆