Question OutOfMemoryException ? How to fix it ?

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
I have a program that keeps getting System.OutOfMemoryExceptions. The program relies on some "heavy" GDI+ drawing

All my drawing code looks basically like this:
VB.NET:
Public Class MyList

    Private fntLucida18 As New Font("Lucida Console", 18)

    Private whiteBrush As Brush = Brushes.White
    Private blackBrush As Brush = Brushes.Black
    Private blackPen As Pen = Pens.Black

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)


        With e.Graphics

            For Each item In MyItems
                .FillRectangle(whiteBrush, x, y, width, height)
                .DrawRectangle(blackPen, x, y, width, height)

                .DrawString("Some text", fntLucida11Regular, blackBrush, x, y)
            Next
        End With
    End Sub

End Class
MyItems could be like 30-40 items. Everything that OnPaint uses is set at the top of the class, they are are never changed or disposed, they stay the same until the program closes (ok, I do dispose them when the form closes, but not before)
I tried adding the fonts, brushes, etc inside the OnPaint method and disposed them after each use, but that seems to slow down the OnPaint sub a bit, also after a very short time it would throw an exception (don't remember the exception, but it was thrown when disposing a brush)

I only use DrawString, DrawRectangle and FillRectangle, nothing else

Does anyone have any idea on how to stop this error from happening? It's not like there's no memory left, there's plenty
 
I currently work on a software which generates whole pages of reports using shapes and properties from a master-details pair of database tables, each properties (border width, background color, etc) for each type of shape is defined as Lua scripting. There are about 200 shapes (including 10 - 20 images) with an average of 4 - 5 properties each, all interpreted in Lua. And none of them ever crashed from an out of memory exception, even on a 512 MB RAM equipped machine (it crashed on 256 MB, but only in another part of the application!).

So I think we can rule out the fact that the GDI+ drawing is too heavy for the machine. I'm thinking it probably has a memory leak, either there or in another part of the application. Have you tried profiling it using programs like SOS or a memory profiler? It should tell you that you got a certain object in way too high number meaning it is not disposed of properly (I've had trouble with events keeping a reference to their event handler's object once).

Also, an OutOfMemoryException can be thrown simply because the .NET decided you used too much memory. I actually made some tests on a low memory machine and I came to the conclusion that it wasn't all of the memory that had to be used, but only a certain proportion of it (I can't remember the numbers and they are probably machine dependent).

Try to give some more code about how you use the MyList class... Maybe it's that code which has an error.
 
It seems like you're right, the exception might not be because of the drawing. Seems like the problem is thread related
I deleted the start thread lines in the code, which stopped the application from crashing

In the MyItems objects I have a thread running in the background (one thread for each object). It seems like these threads are taking memory even after I disposed the objets
In the Sub these threads operate I have something like this:

VB.NET:
While Me.disposedValue = False

' Sleep

' Do some work

End while

With my understanding, calling dispose on the object should cause the thread to end after finishing work
Also the threads are set as background threads

Maybe the objects are not disposed correctly


Looks like I managed to solve the problem. For some weird reason the thread never got out of the above mentioned loop when the object was disposed. A few changes to the loop fixed the problem
 
Last edited:
Back
Top