title: Memory usage in Winforms
body: <p>I have very simple application of two forms (created to test memory). Solution has three projects. 1st having forms, 2nd DAL and 3rd <code>ComonLib</code>.</p>
  
  <p><em><strong>(All these memory figures are from Task Manager. At the end of the question there are 4 urls of images of memory-profiler)</em></strong></p>
  
  <p>When I run my app the memory usage is around <code>8MB</code>. This form only have two buttons. One button is to open 2nd form and another button to collect <code>GC</code>.</p>
  
  <p>When I open 2nd form the memory increase to around <code>550MB</code>. This form has a grid and data (<code>1000s of records</code>) loads in this grid in Load event.</p>
  
  <p>Code to open 2nd form.</p>
  
  <pre><code>CallWidnows cw = new CallWidnows();
  cw.ShowDialog();
  cw.Dispose();
  </code></pre>
  
  <p>Load event of 2nd form</p>
  
  <pre><code>Customers customers = new Customers();
  dataGridView1.DataSource = customers.GetAllCustomers();
  </code></pre>
  
  <p>Close event of 2nd form.</p>
  
  <pre><code>dataGridView1.Dispose();
  </code></pre>
  
  <p>But after closing 2nd form no change in memory usage, and memory usage is still 550MB</p>
  
  <p>After waiting a minute or so I explicitly call <code>GC.Collect()</code> and only few MBs of memory releases and become to <code>530MB</code></p>
  
  <p>Again after waiting a minute or so I agin call <code>GC.Collect()</code> and now memory come down to <code>40MB</code></p>
  
  <ul>
  <li><p>Can anyone help me to understand this
  memory usage behavior?</p></li>
  <li><p><strong>Why on disposing form and grid memory
  is not released?</strong></p></li>
  <li>Why on 2nd time calling
  <code>GC.Collect()</code> all the memory
  released but not when I called it
  first time?</li>
  </ul>
  
  <p>My original application has dozens of forms and user controls and I am going through memory problem in this app.</p>
  
  <ul>
  <li>Can anyone suggest few steps to
  analyze memory usage in Winforms app?</li>
  </ul>
  
  <p>One side quetion:
  Originally I had following method as static method.</p>
  
  <pre><code>Customers customers = new Customers();
  dataGridView1.DataSource = customers.GetAllCustomers();
  </code></pre>
  
  <p>It was like </p>
  
  <pre><code>// Static GetAllCustomers method
  dataGridView1.DataSource = Customers.GetAllCustomers();
  </code></pre>
  
  <ul>
  <li>In my original application all DAL
  methods are static. Static methods
  can be reason of Memory Leak when
  used like above?</li>
  </ul>
  
  <p>Memory Profiler Images:</p>
  
  <p><a href="http://img839.imageshack.us/i/firstimagedialog.png/" rel="nofollow">1- Application Starts Memory usage 8MB</a></p>
  
  <p><a href="http://img859.imageshack.us/i/secondimagedialog.png/" rel="nofollow">2- Second form opens and data loaded - Memory Usage - 550MB</a></p>
  
  <p><a href="http://img857.imageshack.us/i/thirdimagedialog.png/" rel="nofollow">3- Second form closed. Form disposed and grid disposed - Memory Usage still - 550 MB</a></p>
  
  <p><a href="http://img846.imageshack.us/i/fourthimagedialog.png/" rel="nofollow">4- First time GC.Collect called.</a></p>
  
  <p>Thanks.</p>