Visual Basic 垃圾收集
在 VB.NET 中,在使用完对象后将对象分配给 Nothing 是否仍然有用,如前面提到的 这里?或者垃圾收集是否已改进到不再有帮助/必要的程度?
Is it still useful in VB.NET to assign objects to Nothing when finished using them, as mentioned here? Or has garbage collection improved to the point that this is no longer helpful/necessary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,大多数情况下没有必要。
如果您使用完一个对象并希望尽快声明其内存(例如,因为它是一个非常大的实体,其中包含许多其他实体),请使其实现 Disposable 模式并通过 使用指令。
在您的大对象不引用任何非托管资源的特殊情况下,这并不能解决内存泄漏,而是使您的内存占用量保持较小
As the others said, it's not necessary in most cases.
If you are done using an object and want to claim its memory as soon as possible (for example, because it's a very big entity which contains many others), make it implement the Disposable pattern and use it via the Using directive.
In the particular case in which your big object does not reference any unmanaged resource, this is not fixing a memory leak, it's keeping your memory footprint small
不,它在 VB.NET 中没有用。 IIRC 仅当变量指向动态创建的 COM 对象(例如您正在执行 Office Interop)时才应执行此操作。
No, it's not useful in VB.NET. IIRC you should do this only if variables are pointing to dynamically created COM objects such as if you are doing Office Interop for example.
你是对的,除了一些极端情况之外,不再需要它:
为了消除循环引用(对象 A 引用对象 B,对象 B 又引用对象 A)工作正常,请参阅注释。You are right, it's no longer needed except in a few corner-cases:
To eliminate cyclic references (Object A references Object B which references Object A)Works fine, see comments.