I often store certain IDisposables as fields, forcing my class to implement IDisposable. It will thus simply be collected.Īlright, now some actual background info. What about a HttpClient? And how do you know in which situations it does and in which is does notĪnswer: The reference source for HttpClient seems to indicate that HttpClient does not have a finalizer. Examples, MemoryStream does not have a finalizer, whereas FileStream does. It follows the same rule as above, however, so if it doesn't have a finalizer it will simply be collected. Stream by itself is a base class, so depending on the concrete derived class it may or may not have a finalizer. Is it disposed when I close the console app?Īnswer: No, it will not be disposed. 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).īut let's say I have a console app, with a static Stream variable. The finalizer thread is not allowed to run "forever" to do this, however, so it may also run out of time. 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. NET 4.5 (or higher) application closes, what happens to the IDisposables which were not properly disposed?Īnswer: Nothing. (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)Ī 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.Īnyway, with all that under our belt, let's tackle your specific questions: The general rule is that if the object itself owns unmanaged resources it probably should and if it doesn't it probably shouldn't. Now, objects implementing IDisposable may or may not have a finalizer. None of the above relies on IDisposable at all. Note that in the above paragraphs of text, IDisposable is not mentioned once, and there is a good reason for that. 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. 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. 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. Normally, if the object does not have a finalizer, it will be collected during this collection. NET Runtime detects that an object is eligible for garbage collection. NET Runtime that before it can collect the object, it has to execute the finalizer. In most cases (probably preferably all), objects with finalizers also implement IDisposable but they are in fact two distinct things, most often used together.Ī finalizer is a mechanism to say to the. It's important to distinguish between objects implementing IDisposable and objects with finalizers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |