Does the “using” keyword mean the object is disposed and GC'ed?

前端 未结 2 1347
死守一世寂寞
死守一世寂寞 2021-01-01 09:11

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

 //Using keyword i         


        
相关标签:
2条回答
  • 2021-01-01 09:30

    You're talking about two very different things.

    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).

    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.

    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.

    0 讨论(0)
  • 2021-01-01 09:45

    She is right on the money using statement is just a syntatic sugar for try/finally{obj.Dispose();} . using statement ensures that the object will be disposed.(Dispose method will be called) but it has no relationship with garbage collection.

    Take a look at this Understanding-the-using-statement

    Short answer: So now we know using statement is just calling Dispose and does nothing other than that, and keep in mind that Dispose method is no special than any other methods. It is just a method and that's it. So it is no way related to garbage collection. Interestingly "Garbage Collector" doesn't even know about Dispose method or IDisposable.

    Hope this helps

    0 讨论(0)
提交回复
热议问题