<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Bijesh Karuvar's blog</title>
  <link rel="alternate" type="text/html" href="http://www.developerdotstar.com/community/blog/28"/>
  <link rel="self" type="application/atom+xml" href="http://www.developerdotstar.com/community/blog/28/atom/feed"/>
  <id>http://www.developerdotstar.com/community/blog/28/atom/feed</id>
  <updated>2006-12-04T13:23:07-08:00</updated>
  <entry>
    <title>To Dispose or Not to Dispose ?</title>
    <link rel="alternate" type="text/html" href="http://www.developerdotstar.com/community/node/247" />
    <id>http://www.developerdotstar.com/community/node/247</id>
    <published>2005-08-19T04:05:28-07:00</published>
    <updated>2006-12-04T13:23:07-08:00</updated>
    <author>
      <name>Bijesh Karuvar</name>
    </author>
    <category term="Software Development" />
    <category term=".NET" />
    <category term="C#" />
    <category term="VB.NET" />
    <summary type="html"><![CDATA[<p>Dispose() is merely a convenient place for a class developer to place any resource clean-up code, in the hope that clients will call it. Dispose does not free the managed memory allocated for an object.</p>
    ]]></summary>
    <content type="html"><![CDATA[<blockquote><p>From <a href="http://weblogs.asp.net/jarnold/archive/2004/08/10/211969.aspx" title="http://weblogs.asp.net/jarnold/archive/2004/08/10/211969.aspx">http://weblogs.asp.net/jarnold/archive/2004/08/10/211969.aspx</a></p>
<p>Myth:</p>
<p>Dispose() releases an object's memory.</p>
<p>Rationale:</p>
<p>Well, it's called 'Dispose' isn't it?</p>
<p>Truth:</p>
<p>Dispose() is merely a convenient place for a class developer to place any resource clean-up code, in the hope that clients will call it. Dispose does not free the managed memory allocated for an object. The only thing that can do that is the garbage collector; there is no way to deterministically (or manually) release memory.
</p></blockquote>
<p>Now, why should we call Dispose() for datasets and datatables and other disposable objects ?</p>
<p>Let's see an example:</p>
<div class="codeblock">
<pre>Dim dsSomeData as Dataset<br />dsSomeData = objSomeObject.GetSomeData()<br />dsSomeData.Dispose()<br />dgDataGrid.DataSource = dsSomeData<br />dgDataGrid.DataBind()</pre></div>
<p>If the Dispose() actually releases memory and marks it for disposing in the next garbage collection, this should throw an exception. But, guess what? The above code runs fine.</p>
<p>So, what does Dispose() for dataset and datatable actually do? Nothing! You can call the Dispose() method for the dataset, and the dataset still contains the dataset. No release of memory.</p>
<p>The Dispose method in DataSet exists ONLY because of side effect of inheritance-- in other words, it doesn't actually do anything useful in the finalization. The class DataSet inherits from "System.ComponentModel.MarshalByValueCompenent" which implements the IDisposable interface because it is a component. That being the case, it is only used to be treated as a component that you can put on a form. The method is not overridden in the System.Data.Dataset class and as such, it all depends what MarshalByValueCompenent does. Calling Dispose is just going to remove the DataSet from any component site that it's been added to.</p>
<p>This is the disassembled code of MarshalByValueComponent</p>
<div class="codeblock">
<pre>Public Sub Dispose()<br /> Me.Dispose(True)<br /> GC.SuppressFinalize(Me)<br />End Sub</pre></div>
<p>And the internal Me.Dispose(True) does this:</p>
<div class="codeblock">
<pre>Protected Overridable Sub Dispose(ByVal disposing As Boolean)<br />  If disposing Then<br />    SyncLock Me<br />      If ((Not Me.site Is Nothing) AndAlso (Not Me.site.Container Is Nothing)) Then<br />        Me.site.Container.Remove(Me)<br />      End If<br />      If (Not Me.events Is Nothing) Then<br />        Dim handler1 As EventHandler = _<br />          CType(Me.events.Item(MarshalByValueComponent.EventDisposed), EventHandler)<br />        If (handler1 Is Nothing) Then<br />          Return<br />        End If<br />        handler1.Invoke(Me, EventArgs.Empty)<br />      End If<br />    End SyncLock<br />  End If<br />End Sub</pre></div>
<p>Since the DataSet does not have a container, the above code practically doesn't do anything other than telling GC not to call Finalize method. If I understand this correctly, we'll never have to call dispose method on DataSets and DataTables.</p>
    ]]></content>
  </entry>
</feed>
