gpt4 book ai didi

c# - 有关 C# Dispose 工作原理的更多信息

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:29 25 4
gpt4 key购买 nike

在尝试理解 IDisposable 时,我有几个大多数答案都没有明确说明的问题。

  1. 如果我们在一个对象上调用 dispose,是在那个时候释放这个对象还是只是我们想要清理的类成员,然后整个对象会被 GC 销毁。

  2. 我们在 dispose 方法中抑制终结器后,GC 是否仍会清理我们在 dispose 中没有清理的所有类成员?

最佳答案

一旦所有对它的引用都消失了,一个对象就会以一种不确定的方式被垃圾回收。 Garbage collection 来自 MSDN:Garabge collection fundamentals

Garbage collection occurs when one of the following conditions is true:

  1. The system has low physical memory.
  2. The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.

The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

  1. Garabge collection when to use
  2. Understanding Object life cycle

关于c# - 有关 C# Dispose 工作原理的更多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15157877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com