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?


基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01