Does C# app exit automatically dispose managed resources?

故事扮演 提交于 2020-01-23 06:16:13

问题


I am fully aware that using statements are the way to handle IDisposables. Please do not repeat this advice in comments.

When a C# .NET 4.5 (or higher) application closes, what happens to the IDisposables which were not properly disposed?

I know some have a finalizer for disposing unmanaged resources.

But let's say I have a console app, with a static Stream variable. Is it disposed when I close the console app?

What about a HttpClient? And how do you know in which situations it does and in which is does not?

Alright, now some actual background info. I often store certain IDisposables as fields, forcing my class to implement IDisposable. The end user should use using. But what if that does not happen?

Is it merely unnecessary memory until GC? Or do you suddenly have a memory leak?


回答1:


It's important to distinguish between objects implementing IDisposable and objects with finalizers. In most cases (probably preferably all), objects with finalizers also implement IDisposable but they are in fact two distinct things, most often used together.

A finalizer is a mechanism to say to the .NET Runtime that before it can collect the object, it has to execute the finalizer. This happens when the .NET Runtime detects that an object is eligible for garbage collection. Normally, if the object does not have a finalizer, it will be collected during this collection. If it has a finalizer, it will instead be placed onto a list, the "freachable queue", and there is a background thread that monitors this thread. Sometimes after the collection has placed the object onto this queue, the finalizer thread will process the object from this queue and call the finalizer method.

Once this has happened, the object is again eligible for collection, but it has also been marked as finalized, which means that when the garbage collector finds the object in a future collection cycle, it no longer places it on this queue but collects it normally.

Note that in the above paragraphs of text, IDisposable is not mentioned once, and there is a good reason for that. None of the above relies on IDisposable at all.

Now, objects implementing IDisposable may or may not have a finalizer. The general rule is that if the object itself owns unmanaged resources it probably should and if it doesn't it probably shouldn't. (I'm hesitant to say always and never here since there always seems to be someone that is able to find a cornercase where it makes sense one way or another but breaks the "typically" rule)

A TL;DR summary of the above could be that a finalizer is a way to get a (semi-)guaranteed cleanup of the object when it is collected, but exactly when that happens is not directly under the programmers control, whereas implementing IDisposable is a way to control this cleanup directly from code.

Anyway, with all that under our belt, let's tackle your specific questions:

When a C# .NET 4.5 (or higher) application closes, what happens to the IDisposables which were not properly disposed?

Answer: Nothing. If they have a finalizer, the finalizer thread will try to pick them up, since when the program terminates, all objects become eligible for collection. The finalizer thread is not allowed to run "forever" to do this, however, so it may also run out of time. If, on the other hand, the object implementing IDisposable does not have a finalizer it will simply be collected normally (again, IDisposable has no bearing at all on garbage collection).

But let's say I have a console app, with a static Stream variable. Is it disposed when I close the console app?

Answer: No, it will not be disposed. Stream by itself is a base class, so depending on the concrete derived class it may or may not have a finalizer. It follows the same rule as above, however, so if it doesn't have a finalizer it will simply be collected. Examples, MemoryStream does not have a finalizer, whereas FileStream does.

What about a HttpClient? And how do you know in which situations it does and in which is does not

Answer: The reference source for HttpClient seems to indicate that HttpClient does not have a finalizer. It will thus simply be collected.

Alright, now some actual background info. I often store certain IDisposables as fields, forcing my class to implement IDisposable. The end user should use using. But what if that does not happen?

Answer: If you forget/don't call IDisposable.Dispose() on objects implementing IDisposable, everything I've stated here regarding finalizers will still happen, once the object is eligible for collection. Other than that, nothing special will happen. Whether the object implements IDisposable or not have no bearing on the garbage collection process, only the presence of a finalizer has.

Is it merely unnecessary memory until GC? Or do you suddenly have a memory leak

Answer: Undetermined from this simple information. It depends on what the Dispose method would do. For instance, if the object has registered itself somewhere so that there is a reference to it, somewhere, for some code to stop using the object may not actually make the object eligible for collection. The Dispose method might be responsible for unregistering it, removing the last reference(s) to it. So this depends on the object. Merely the fact that the object implements IDisposable does not create a memory leak. If the last reference to the object is removed, the object becomes eligible for collection and will be collected during a future collection cycle.


Remarks:

  • Note that the above text is also probably simplified. A full collection cycle to actually "collect memory" is probably not done on application termination as there is no point. The operating system will free the memory allocated by the process when it terminates anyway. Probably only finalization is performed. (meaning, I have no knowledge one way or another what kind of optimizations is done here)

  • The more important part here is that you need to distinguish between memory (or other) leaks during program execution and after program execution

    • When the process terminates, the operating system will reclaim all memory allocated to it, it will close all handles (which may keep sockets, files, etc. open), all threads will be terminated. In short, the program is completely removed from memory
    • The process may have left tidbits of itself around though, which are not cleaned up unless the process took care to do this beforehand. An open file is closed, as stated above, but it may not have been completely written and thus may be corrupt in some way.
    • During program execution, leaks may make the program grow in terms of allocated memory, it may allocate too many handles because it fail to close the ones it no longer needs, etc. and this is important in terms of handling IDisposable and finalizers correctly, but when the process terminates, this is no longer a problem.



回答2:


The process is about to disappear. That means the OS is going to stop providing any memory to support that process's address space1. All memory is "reclaimed" anyway. It's also going to terminate all threads within the process. So no further processing will occur. All handles will be closed anyway.

The things you have to worry about are the external realities. E.g. if you have an undisposed Stream, has it flushed everything it needs to if it's attached to something over the network or a file?

Yes, if you're storing Disposables yourself, you should implement IDisposable. But if you don't have any unmanaged resources, don't implement a finalizer. By the time a finalizer runs, you shouldn't be accessing any other managed objects from that code, so it's too late to dispose of them.

If someone forgets to Dispose your objects, that's their error, and you've done as much as you should by advertising that you should be disposed via the IDisposable interface.


1Processes don't have memory. They have an address space managed by the OS. At times, it may be necessary that some parts of that address space are in memory. That's the OS's job in the age of virtual memory, and it only every really "loans" the physical memory temporarily to any given process. It can take it away again at any time




回答3:


Nothing gets disposed automatically, ever.

Classes that implements the IDisposable interface are designed like that because they either use IDisposable fields (like in your case) or the use unmanaged resources (There are exceptions to this rule, but that's out of scope for this answer).

There is no part of the CLR that calls the Dispose method.
The GC will collect the reference and unless instructed otherwise (by using GC.SuppressFinalize(), will then move the reference to the finalizer queue, where it will finalized by calling it's finalize method.
If, and only if, the class have explicitly override the finalize method and calls Dispose in the finalize method, then the instance will finally be disposed.

So, if you want to ensure your classes gets disposed by the finalizer, you must override the finalize method in your class. However, Beware - Implementing the Finalize method correctly is hard!

That being said, when you implement the IDisposable interface, you are telling whoever is using this class that it should be disposed. Whether they actually dispose it or not is no longer your responsibility - it's theirs. So if there actually is a memory leak (and that's quite possible there will be one) - assuming your class implemented the IDisposable interface correctly, that's not your problem.




回答4:


Does C# app exit automatically dispose managed resources?

Well, yes and no, but technically: no.

If the IDisposable interface is implemented correctly, the dispose function will be called when the object is collected. If it isn't correctly implemented, there will be a leak if unmanaged resources are used.

When a C# .NET 4.5 (or higher) application closes, what happens to the IDisposables which were not properly disposed?

The process space is freed. If the IDisposable implicitly reserves memory in other process spaces, and is not correctly disposed: you have a leak.

But let's say I have a console app, with a static Stream variable. Is it disposed when I close the console app?

It is, although implicit, it can lead to unexpected result for example when you kill the process. For example: a TCP port might be in a wait state.

What about a HttpClient? And how do you know in which situations it does and in which is does not?

Same as above.

Alright, now some actual background info. I often store certain IDisposables as fields, forcing my class to implement IDisposable. The end user should use using. But what if that does not happen?

You should implement it correctly, but advise to dispose. For example a file handle. If it isn't properly disposed, another call to the file might fail because the file is still open. This might be resolved at GC, but you don't know when.

Is it merely unnecessary memory until GC? Or do you suddenly have a memory leak?

It more, leaks, open ports, open handles, video memory consumption etc.


For a correct implementation of IDisposable see: Proper use of the IDisposable interface

If you read it, you can imagine not all 3th party lib's are implementing it correctly.



来源:https://stackoverflow.com/questions/52644065/does-c-sharp-app-exit-automatically-dispose-managed-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!