Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Disposal of Singleton IDisposable Object

Tags:

c#

.net

Scenario:

An application instantiates a single instance of a class that implements IDisposable. The instance is exposed through a static member that makes it accessible to all parts of the application. The single instance needs to be kept alive for the lifetime of the application.

Question

How critical is it that the Dispose method be called before the application shuts down and the process terminates?

I've always believed that in most scenarios like this, it is not necessary to call Dispose because the termination of the process naturally cleans up the resources. Am I wrong?

like image 907
Dan Avatar asked Sep 26 '12 03:09

Dan


People also ask

How do you use the dispose method?

The Dispose() methodThe Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.

Where are singleton objects stored?

Singleton objects are stored in Heap, but static objects are stored in stack. We can clone the singleton object, but we cannot clone the static class object . Singleton classes follow the OOP concepts), static classes do not.

Is IDisposable called automatically?

By default, the garbage collector automatically calls an object's finalizer before reclaiming its memory. However, if the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer.

When the Dispose method is called?

It only occurs when there are objects in the Finalization Queue. It only occurs when a garbage collection occurs for Gen2 (which is approx 1 in every 100 collections for a well-written . NET app).


2 Answers

If by "your application shuts down" you mean the process is terminating, then you technically don't need to do anything. Your process is terminating and the OS is going to free up those resources.

I imagine their could be some obscure corner cases where some component could create some type of file or other resource and if Dispose is not called, that won't necessarily be cleaned up, even if your process terminates.

I'd like to give an example of this, albeit this is a bizarre corner case. Let's say you reference and use a component in your code. When you create and use this, it creates a 2GB file on your machine. Now let's take it a step further and say that this component, actually closes the file handle itself accessing this 2GB file sometime during it's use, due to a bug or just bad design. Now the Dispose method on this object cleans up this file, but it is not well-documented. Essentially, missing the Dispose call to this will leave a file on the machine. This is definitely a corner case and won't cause your machine to "leak" anything, but you do have a 2GB file just sitting there.

That being said - the best practice is to call Dispose when you are deterministically done with your resource(s). You could call a method on your Singleton - let's say Cleanup() - that can run when you are shutting down.

like image 71
Bryan Crosby Avatar answered Nov 15 '22 16:11

Bryan Crosby


This post has detailed explaination on why disposing of event pub Others is a good practice

.NET object events and dispose / GC

As there are corner cases where gc will not collect Publisher and it will keep firing events even after set to null.

like image 23
NiladriBose Avatar answered Nov 15 '22 16:11

NiladriBose