readonly 修饰符对垃圾收集器有影响吗?
readonly 修饰符是否有可能影响垃圾收集器的工作? 对于值类型,对于引用类型?
Is it possible that readonly modifier influences on Garbage collector work?
For value type, for reference type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,事实并非如此。 GC 的工作原理是回收无法从任何根对象访问的对象的内存。 readonly 修饰符对此过程没有影响。将以相同的方式收集两个对象图,除了一个对象图具有一些只读字段之外,这两个对象图是相同的。
No it does not. The GC works by reclaiming memory for objects which are not reachable from any of the rooted objects. The readonly modifier has no impact on this process. Two objects graphs which were identical with the exception of one having a few readonly fields would be collected in the same fashion.
我不明白为什么它会影响正常使用的 GC。这只是一个领域。 GC 仅遵循引用类型字段(或结构字段中包含的引用)。
您也许能够构建影响 GC 的人为场景(可能滥用只读字段的防御副本),但正常使用时不会发生这种情况。
当然,您不能在像
Dispose
这样的方法中将该字段设置为 null,如果您希望为 GC 准备昂贵的自有对象,同时某些对象保存对包含对象的引用,那么这可能会很有用。由于
只读
而改变程序行为的邪恶示例该程序表明,仅将字段从非只读更改为只读就可以阻止使用其他相同代码收集对象。它滥用在方法调用时复制只读字段,以将可写结构中的 Obj 字段设置为 null 并将引用保留在只读结构中。由于它可以防止引用变为 null,因此可以防止对象被收集。
但这当然不会影响 GC 本身。相反,滥用只读语义来创建具有只读功能和不具有只读功能的不同对象图。所以贾里德的说法仍然是完全正确的。
I don't see why it should influence the GC with normal use. It's just a field. And the GC simply follows reference type fields(or references contained in struct fields).
You might be able to construct artificial scenarios where it influences the GC(perhaps abusing defensive copies of the readonly field), but it won't occur with normal usage.
And of course you can't set the field to null in a method like
Dispose
which might be useful if you want to have expensive owned objects ready for GC while something holds a reference to the containing object.Evil sample of a program changing behavior due to
readonly
This program shows that just changing a field from not readonly to readonly can prevent collection of an object with otherwise identical code. It abuses that a readonly field gets copied on method invocation to set the Obj field null in the writable struct and keep the reference in the readonly struct. Since it prevents the reference from becoming null it prevents the object from being collected.
But of course this doesn't influence the GC itself. But instead abuses the semantics of readonly to create a different object graph with readonly than without. So Jared's statement is still completely true.
readonly
是一项 C# 功能。 GC 是 CLI 功能。因此,它根本不能影响GC。。readonly
is a C# feature. The GC is a CLI feature. Therefore, it simply cannot influence the GC at all.