请问"使用"关键字的意思是对象被销毁和GC'ed?关键字、对象、意思、QUOT

2023-09-03 05:35:46 作者:不合时宜.

我搭讪今天我的同事,谁说,她刚刚学会使用使用语句背后的原因。

I struck up a conversation with my colleague today, who said she'd just learned the reason behind using the using statement.

 //Using keyword is used to clean up resources that require disposal (IDisposable interface). 

 using (StreamReader reader = new StreamReader(@"C:\test.txt")) 
 { 
 string line = reader.ReadLine(); 
 } 

我指出,该对象被标记为可设置,但实际上没有处理和垃圾收集,除非GC决定这样做。

I pointed out that the object is marked as "Can be disposed" but not actually disposed and garbage-collected unless GC decides to do so.

她回答说,对象将被自动一旦使用语句结束,因为using语句被翻译成的try-catch-finally块处置。因此,该对象必须得到设在using语句的末尾。

She responded that the object will be disposed automatically once that using statement ends because the using statement is translated to try-catch-finally block. So, the object must get disposed at the very end of the using statement.

我被这个困惑,因为我知道,使用使用语句不保证该对象被GC回收。所有这一切发生的是,的Dispose()方法将被调用。在GC决定什么时候,无论到​​GC它。但是,当她要求的证明,我找不到任何。

I was confused by this, because I know that using a using statement does not guarantee that the object gets GC-collected. All that happens is that the Dispose() method would be called. The GC decides when to GC it regardless. But when she asked for proof, I could not find any.

有谁知道这是如何工作的,以及如何证明呢?

Does anyone know how this works, and how to prove it?

推荐答案

你说的是两个完全不同的事情。

You're talking about two very different things.

对象将尽快使用 - 块结束处理。这并没有说明当垃圾回收任何东西。被释放的唯一一次堆内存是当垃圾回收时 - 这只有在内存pressure发生(除非你使用 GC.Collect的明确)

The object will be disposed as soon as the using-block ends. That doesn't say anything about when it is garbage collected. The only time heap memory is released is when a garbage collection occurs - which only happens under memory pressure (unless you use GC.Collect explicitly).

处置的对象只是意味着调用它的处置方法。最经常意味着释放无论是稀缺资源,或本地资源(在实践中,所有的稀缺资源是本土 - 插座,文件,...)。现在,它的方便,在你的情况下,可支配对象的生命周期范围是有限的,所以理论上可以尽快使用 - 块结束收集 - 但是,并没有真正发生在实践中,因为.NET运行时尽量避免藏品 - 他们是昂贵的。所以,直到你穿越记忆的配置的门槛,没有集合的事情发生,即使你有堆死的对象。

Disposing an object simply means calling its Dispose method. That most often means releasing either a scarce resource, or a native resource (in practice, all scarce resources are native - sockets, files, ...). Now, it's handy that the lifetime of the disposable object in your case is limited in scope, so it could theoretically be collected as soon as the using-block ends - however, that doesn't really happen in practice, since the .NET runtime tries to avoid collections - they're expensive. So until you cross a memory allocation threshold, no collection is going to happen, even though you have a dead object on the heap.

那么,什么是处置的地步?无关,与托管内存。你真的不关心管理的内存,你不应该指望处置将真正被称为 - 它并不一定要。是的唯一拥有的运行时堪称是终结,你永远只能利用它来进行处理的天然资源 - 事实上,有没有保证,如果对象有一个引用还是托管的内存可能已经被开垦,然后 - 由终结运行时间存在。这就是为什么你的永远的一个终结处理管理的资源。

So what's the point of Dispose? Nothing to do with managed memory. You don't really care about managed memory, and you shouldn't expect that Dispose will actually be called - it doesn't have to be. The only thing that has to be called by the runtime is the finalizer, and you can only ever use that for disposing of native resources - in fact, there's no guarantee if the objects you have a reference to still exist by the time the finalizer runs - the managed memory might have already been reclaimed by then. That's why you never handle managed resources in a finalizer.

所以,是的,她是完全正确的。问题是,的IDisposable 无关的垃圾收集器。处置并不意味着垃圾收集。

So yes, she was completely right. The point is that IDisposable has nothing to do with the garbage collector. Disposed does not mean garbage collected.