c# - Does the "using" keyword mean the object is disposed and GC'ed? -


I had a conversation with my colleague today, who said that it is the only statement.

  // Keywords are used to clear resources that require a settlement (IDisposable interface). (Using the StreamReader reader = new stream reader (@ "c: \ test.txt")) {string line = reader.ReadLine (); }   

I have indicated that the object is marked as "can be disposed" but it has not been dealt with in reality and unless GC decides to do so, the waste Does not accumulate

He answered that the object will be disposed off automatically once, which ends the statement, because the statement used can be translated, translated into the block at the end. Therefore, the object should be used at the very end of the statement.

I was confused by this, because I know that using a statement using the statement does not guarantee that the object gets collected-whatever happens That the disposable () method will be called. GC decides how long GC can be. But when he asked for the evidence, I could not find any.

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

You are talking about two very different things.

By using

the object will be disposed of. It does not say anything about garbage collection. Only heap memory is released at a single time when there is a garbage collection - which is only under memory pressure (unless you explicitly use g.cclit ).

Its settlement method means it is often meant to issue a rare resource, or a native resource (in practice, all rare resources root-sockets, files, ...) . Now, it is easy that in your case the lifetime of the disposable object is limited, so it can theoretically be used to collect - -black ends by using - though Actually not practiced, as .NET tries to avoid runtime collections - they are expensive. So unless you cross the memory allocation threshold, there is no archive going on, even if you have a dead object on the heap.

then what happens to the point ? There is nothing to do with the managed memory, you really do not care about the managed memory, and you should not expect that dissection will actually be told - this is not necessarily the only thing that can be done. That is going to finalize called by runtime, and you can only use it to settle the basic resources - in fact, there is no guarantee that the goods Your reference is still present from the last running time - managed memory can probably be retrieved by this time, this is the reason why you never manage the managed resources in the final form.

So yes, that was completely right. The point is that IDisposable does not have to do anything with the garbage collector. Disposed means the garbage is not collected.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -