Click here to Skip to main content
15,445,560 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear CodeProject Members,

I have been facing serious memory issues and they are driving me nutes. Currently I am making a terraria like game, as you can see on this video:[^]

I have been able to make a fully working chunk system, and so far have updated it so that I can have about 250 different kind of blocks. Although here is not where my problem lies I seem to be only gaining memory. It's driving me nuts and I have no idea why. This is what I mean:

I tried drawing only with the graphics from the Paint Event itself but this resulted the same with even more memory. Is this normal or am I seriously messing something up?

Here is my piece of code:

protected override void OnPaint(PaintEventArgs e)
    // -- Clear.

    // -- Draw on bitmap..
    Bitmap _Bitmap = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
    using (Graphics g = Graphics.FromImage(_Bitmap))


    // -- Draw the Bitmap.
    e.Graphics.DrawImage(_Bitmap, 0, 0, this.ClientSize.Width, this.ClientSize.Height);

    // -- Dispose the bitmap.

    // -- Flush.

    // -- Variables.

The funny thing is, is, that without even going out of the first chunk the issue still occurs, so it is not a chunk error. Which I have also checked over 100+ times.

Let me also add that this memory does not drop down to it's original amount after a while, people told me that.

Anyone ? Help ! :L
Updated 19-Dec-12 7:38am
Yvar Birx 19-Dec-12 14:39pm    
Someone please help.
Mike Hankey 19-Dec-12 14:42pm     CRLF
Curious you are disposing of the bitmap after you're through with it...hmmm If the bitmap is the same size you might try creating it once and keeping it as a class variable so you don't have to create it every time. Is there something in one of the other draw routines that is causing the problem? You might try commenting them out one by one and seeing if the problem lies there?

1 solution

First, do NOT look at Task Manager to tell you how much memory your app is using. It's lying to you. It's showing you how much memory the .NET CLR has RESERVED, not how much your app is actually using.

Get rid of the GC stuff, you don't need it. You actually don't need to draw to a Bitmap object either. All you would have to do is pass the Graphics object you got from the event args to your DrawXxx methods.

If you want to see what your app is actually using, use PerfMon and the .NET Memory counters.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900