Ehsanit
Well-known member
- Joined
- Feb 23, 2011
- Messages
- 80
- Programming Experience
- 1-3
I'm working on a simple program to project some numbers onto a screen, and it seemed to be working well. However I've since realized that there's a serious memory leak, adding over 4MB to the RAM usage every time the numbers update (which can be once a second or even faster)!
The problem code is as follows:
I realize that the problem arises on the first line when I set BMP to a new (transparent) bitmap the size of my screen without removing the previous one. However I cannot work out how to remove the previous one at all, nor why it isn't being removed.
The problem code is as follows:
Private Sub RefreshNumbers() BMP = New Bitmap(Me.Width, Me.Height) ' Transparent Background for painting numbers onto Gr = Graphics.FromImage(BMP) ' Unlocks graphics commands including painting onto BMP For Corner As Byte = 0 To CByte(DispMode) ' Draws between 1 and 4 numbers onto the blank bitmap Dim StringSize As SizeF = Gr.MeasureString(DispStrings(Corner), New Font("Seguo Script", TextSize, System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic)) Gr.DrawString(DispStrings(Corner), New Font("Seguo Script", TextSize, System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), Brushes.Green, CSng(SectionLeft(Corner) + ((SectionWidth - StringSize.Width) / 2)), CSng(SectionTop(Corner) + ((SectionHeight - StringSize.Height) / 2))) Next Me.BackgroundImage = BMP ' Displays the bitmap. Me.Refresh() End Sub
I realize that the problem arises on the first line when I set BMP to a new (transparent) bitmap the size of my screen without removing the previous one. However I cannot work out how to remove the previous one at all, nor why it isn't being removed.
Last edited: