Garbage Collection: Is it necessary to set large objects to null in a Dispose method?(垃圾回收:是否需要在 Dispose 方法中将大对象设置为 null?)
问题描述
在实现Dispose()
方法时,是否需要将大对象设置为null
?
Is it necessary to set large objects to null
when implementing a Dispose()
method?
推荐答案
通常不会.
垃圾收集器查找有根对象,如果两个对象都没有根,循环依赖不会阻止收集.
The garbage collector looks for rooted objects, and circular dependencies don't prevent collection if neither object is rooted.
有一个警告:如果对象 A 具有对对象 B 的引用,并且对象 B 正在被释放,您可能需要清理该关系,否则您可能会导致泄漏.这个表面最常见的地方是在事件处理程序中(来自 A->B 的引用是 B 控制的,因为它订阅了 A 上的事件).在这种情况下,如果 A 仍然有根,那么即使 B 已经被释放,也无法回收.
There is a caveat: if object A has a reference to object B, and object B is being disposed, you may want to clean up that relationship or else you could end up with a leak. The most common place this surfaces is in event handlers (the reference from A->B is one that B controls, because it subscribed to an event on A). In this case, if A is still rooted, B cannot be collected even though it's been disposed.
这篇关于垃圾回收:是否需要在 Dispose 方法中将大对象设置为 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:垃圾回收:是否需要在 Dispose 方法中将大对象设置为 null?


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01