I've created a program that uses
System.Graphics to render a simple (small) 2D scene to the screen. The problem is, for some reason, it starts to quickly accumulate memory. In fact, it goes from only 100,000K Mem to ten times that and then crashes. As expected, the frame-rate of the program drops dramatically as well (100+ to 1fps).
I believe the culprit is in my rendering loop. Problem is, as hard as I search, besides a couple bits of messy code, I can't find anything relevant to the problem.
It may not be inside of this code snippet - if not, that's ok. I'll just continue my search personally through my application.
Here is my
Public Class Engine Public debugEnabled As Boolean = False Private _currentMap Public Property currentMap As Map Get Return _currentMap End Get Set(ByVal value As Map) _currentMap = value luaEngine.Close() cTime.Reset() sprites.Clear() cTime.Start() luaEngine = New Lua End Set End Property Public currentData As MemoryGrid Public currentInstance As MemoryGrid Public fForm As Control Public cTime As New Stopwatch Public buffer As Bitmap Friend Shared luaEngine As New Lua Public sprites As New Dictionary(Of String, Bitmap) #Region "EVENTS" Public Event event_keydown(ByVal key As String) Public Event event_keyup(ByVal key As String) #End Region Public Sub Render() Dim fpsTimer As New Stopwatch fpsTimer.Start() If fForm IsNot Nothing Then If buffer IsNot Nothing Then Dim e As Graphics = fForm.CreateGraphics() e.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor e.DrawImage(buffer, New Rectangle(0, 0, fForm.Width, fForm.Height)) buffer.Dispose() End If buffer = New Bitmap(fForm.Width, fForm.Height) Using gfx As Graphics = Graphics.FromImage(buffer) gfx.FillRectangle(Brushes.Black, 0, 0, fForm.Width, fForm.Height) If currentMap IsNot Nothing Then For z As Integer = 0 To Grid.LAYERLIMIT For x As Integer = currentMap.DisplayXOffset To currentMap.DisplayXOffset + Math.Ceiling(fForm.Width / 8) For y As Integer = currentMap.DisplayYOffset To currentMap.DisplayYOffset + Math.Ceiling(fForm.Height / 8) Dim terrObj As GridElement = currentMap.Level.getCell(z, x, y) If terrObj IsNot Nothing Then Dim _position As Point Dim _size As Size terrObj.tileImage(Me, _position, _size) Dim cSprites As Bitmap = Nothing : sprites.TryGetValue(terrObj.SpriteSheet & ".png", cSprites) gfx.DrawImage(cSprites, (x - currentMap.DisplayXOffset) * 8 + terrObj.XOffset, (y - currentMap.DisplayYOffset) * 8 + terrObj.YOffset, New RectangleF(_position.X, _position.Y, _size.Width, _size.Height), Drawing.GraphicsUnit.Pixel) End If Next Next Next End If gfx.DrawString("FPS: " & Math.Round(1000 / fpsTimer.ElapsedMilliseconds), New Font("Arial", 7), Brushes.Red, New PointF(5, 5)) fpsTimer.Stop() End Using End If End Sub
There are a couple more procedures after this, but, they are related to my Lua implementation. The problem has existed since before I implemented Lua.
Basically, the variables are at the top, I also have my
Sub Render() at the bottom. If you need any of the other code, I'll make sure to post it. Just say so in the comments.
REMEMBER: I'm not 100% sure that the issue lies in this one class (or in this class at all). If you can't find a reason, so be it. Anything to speed it up (if perhaps my messy programming has slowed it down that bad) would also be appreciated.
Please take a look at the following screenshot of me using ANTS Memory Profiler:
I have a multidimensional array that contains instances of a class called 'GridElement'. The array is 3D. I use it to layer and get elements from specific locations to display. According to this though, it says it takes of like 94% of my application's memory, and for the most part, it's filled with empty slots. The highest number of elements I've put into it is like 20-30 (which is a fraction of what it needs to be able to handle).
So basically, how is this empty 3D array taking up so much space when it has only a couple of elements in it?