Reputation: 15928
If I do a conn.Dispose(); (where conn is an instance of a SqlConnection Class), will that clear the conn object from the heap?
Upvotes: 5
Views: 5564
Reputation: 81159
The purpose of IDisposable is to provide a standard means of notifying objects that they are no longer needed. Some objects ask other entities (which may or may not even be on the same computer!) to do things on their behalf until further notice. When an object is told that it is no longer needed, it can notify any entities that may be acting on its behalf that they no longer need to do so.
As a simple example, creating a file object may (indirectly) cause a request to be sent out to a file server to open a particular file for exclusive access. Until the file server receives a request to close the file, it will not allow anyone else in the known universe to access it. As long as the file object is needed, it will maintain exclusive access to the file. If IDisposable.Dispose is called on the file object, it will (again, indirectly) cause a "close" request to be sent to the file server, thus allowing other programs and computers to access the file.
Note that if the file object were simply to disappear without notifying anyone that the previously-granted exclusive file access was no longer needed, the server might never let anyone else use the file. To protect against this type of problem, the file object's class may define a Finalize() handler. Any time the system creates an object of a class which overrides Object.Finalize()
, the system will add it to a special list of objects with "registered" Finalize handlers. If such an object is abandoned, the garbage-collector will run its Finalize method before it, or any objects to which it holds direct or indirect references, can actually be removed from memory.
Note that the purpose of Dispose is not to destroy the file object itself, but rather to allow it to notify the entity was providing exclusive access the file that such access is no longer needed. Calling Dispose on an object whose class overrides Object.Finalize()
will notify the garbage-collector that it no longer needs to worry about invoking the Finalize method before removing the object from memory, but won't otherwise destroy the object.
Upvotes: 1
Reputation: 110111
From msdn: IDisposable.Dispose
Use this method to close or release unmanaged resources such as files, streams, and handles held by an instance of the class that implements this interface.
So, Dispose is about underlying resources that are outside the view of the garbage collector. Dispose is not about the instance.
will that clear the conn object from the heap?
The SqlConnection instance will Dispose its unmanaged connection resource (by returning it to the connection pool). The SqlConnection instance does not "Dispose" itself from memory - it's a managed object and the Garbage Collector is responsible for that work.
Upvotes: 1
Reputation: 13335
No.
IDisposable provides a well known interface for the 'disposal' of objects. Disposal is generally for the purpose of explicitly releasing resources that would otherwise be held until the object fell out of scope and was garbage collected, which is when an object is removed from memory (heap or stack, almost always heap).
Slightly off topic, you should also be aware that the using
keyword works with IDisposable
e.g.
using (var disposableObject = new DisposableObject())
{
..do stuff with disposableObject..
} //disposableObject.Dispose() implicitly called here.
Upvotes: 1
Reputation: 887453
No; that's the garbage collector's job.
You cannot explicitly interact with the heap at all.
Calling Dispose()
closes expensive resources (in this case, a network connection) deterministically.
Upvotes: 1
Reputation: 11051
No. see msdn The object still lives, until all references are gone. A small example:
myObject = new Something();
myObject.Dispose()
myObject.Foo() // still perfectly valid (but pretty ugly)
myObject = null; // now the reference, while still living, could be collected.
GC.Collect(); // now it is quite sure gone. (never call it unless you have good reasons)
Upvotes: 1
Reputation: 1038810
No, calling Dispose doesn't clear the connection from the heap. When you call the Dispose method on a SqlConnection instance all you do is return the connection to the underlying connection pool. It doesn't even close the connection. ADO.NET uses a connection pool. So when you create a new instance of SqlConnection you do not open a new connection, you simply draw a connection from the connection pool and when you call Dispose you simply return this connection to the connection pool so that it can be reused.
In general the IDisposable
pattern in .NET is intended to be implemented by classes that hold some pointers to some unmanaged resources. Calling the Dispose method ensures that those unmanaged resources will be properly released.
Deleting an object from the heap is what the Garbage Collector does and when this happens is a non-deterministic event (you have no control over it).
Upvotes: 13