title: Out of Memory Exception at System.Drawing.Graphics.FromHdcInternal but no memory leak
body: <p>Am getting a very occasional crash with the following trace:</p>
  
  <pre><code>System.OutOfMemoryException: Out of memory.
     at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
     at System.Windows.Forms.PaintEventArgs.get_Graphics()
     at System.Windows.Forms.Control.PaintBackColor(PaintEventArgs e, Rectangle rectangle, Color backColor)
     at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
     at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
     at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
     at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
     at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
     at System.Windows.Forms.Control.WmEraseBkgnd(Message&amp; m)
     at System.Windows.Forms.Control.WndProc(Message&amp; m)
     at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
     at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)
     at System.Windows.Forms.UserControl.WndProc(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  </code></pre>
  
  <p>As you can see theres none of my code in the trace so i'm finding it very hard to find the cause. Google searches seem inconclusive but generally point to a GC handle leak somewhere but following the most recent crash my memory usage was:</p>
  
  <pre><code>Handle count:16,283, 
  Private Bytes:995,440K, 
  Virtual Bytes:1,628,208K, 
  Working Set:866,892K, 
  GC Heap Size:158,841K, 
  GDI Objects:402, 
  User Objects:1,607 
  </code></pre>
  
  <p>Which does not seem out of the ordinary. Also, I regularly use .net memory profiler to manage leaks.</p>
  
  <p>Unfortunately my app is quite large with many windows so my first question would be: How can I identify which window is causing all the pain?</p>
  
  <p>and then of course my second question would be: If there is no handle leak, what is causing the exception!?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Sorry I can't post any code: Its a massive codebase and the exceptions arent exactly giving me any clues as to which part may be the problem.</p>
  
  <p>I read about there being a 10,000 limit on handles but this app has historically always run fine with 15,000 so I assumed the limit was on something else: GDI handles or user objects maybe?</p>
  
  <p>Just to make sure, I checked and the handles are not being leaked as they are all assigned at startup and do not increase with usage.</p>
  
  <p>Let me modify my question: Given this information, what should the next course of action be? I have process Explorer installed and have succeeded in getting a full memory dump from one of the crashes but really dont have experience in using either to diagnose this kind of problem (until now .net memory profiler has been enough)</p>