Tutorial :System.OutOfMemoryException: Out of memory (GDI)


After installing my program on a windows vista premium, I'm getting the following exception.

The view that must be shown contains following controls: 2 textboxes, 3 labels, a button and linkbutton.

System.OutOfMemoryException: Out of memory.     at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)     at System.Windows.Forms.PaintEventArgs.get_Graphics()     at System.Windows.Forms.Control.PaintException(PaintEventArgs e)     at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)     at System.Windows.Forms.Control.OnPrint(PaintEventArgs e)     at System.Windows.Forms.Control.WmPrintClient(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.Form.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)  

Somebody had the same issue? How to solve it?


This is not necessarily an OOM error, GDI has a habit of throwing Out Of Memory whenever it throws an exception.

To quote Microsoft "GDI+ likes to return OutOfMemoryExceptions in cases that have nothing to do with memory". A nasty little 'idiosyncracy'

See here for details


Does your app make use of any custom controls or controls you've written yourself? Can you repro this problem with a very simple form?



... and this...


... appears to be a similar problems. However they generally talk about custom controls that are failing to dispose object (and, as such, leak GDI handles).

Is it possible that somewhere else in your app you're leaking handles?


Might it be possible that you only detected this on a Vista box because there is less free memory than on your Windows XP boxes? If the machines are roughly the same spec then I would guess the Vista box would have less memory free and therefore highlight issues with memory leaks more quickly.

The other possibility is that you are trying to render too much, as the call stack states there is a scrollable control, is it possible you are rendering a bunch of stuff that isn't actually visible?


How often are you displaying this form? It could be a handle problem with window handles (or lack of).

I've worked on projects that contain dialogs with hundreds of controls and there were memory/handle issues as the dialogs were not Disposed after use.

To work around this, they originally stored the handles for every control on the form the first time the form was displayed and reused them every time after.

In my opinion, it easier and simpler to just destroy what you don't need as soon as you've finished.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »