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?
基础教程推荐
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 将数据集转换为列表 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
