Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of Memory Exception at System.Drawing.Graphics.FromHdcInternal but no memory leak

Am getting a very occasional crash with the following trace:

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& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.UserControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

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:

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 

Which does not seem out of the ordinary. Also, I regularly use .net memory profiler to manage leaks.

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?

and then of course my second question would be: If there is no handle leak, what is causing the exception!?

Edit:

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.

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?

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.

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)

like image 297
dazeddy Avatar asked Mar 02 '11 10:03

dazeddy


1 Answers

You are probably assigning Brushes or Pens and aren't disposing of them - That consumes GDI handles and at some point, all of them are used and you get an OutOfMemoryException.

like image 180
Femaref Avatar answered Oct 16 '22 02:10

Femaref