Click here to Skip to main content
15,883,777 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:
https://www.youtube.com/watch?v=bQ_9m-KkdBQ[^]

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:

http://i49.tinypic.com/2n7f32t.png

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:

C#
protected override void OnPaint(PaintEventArgs e)
{
    // -- Clear.
    e.Graphics.Clear(this.BackColor);

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

        DrawWorld(g);
        DrawPlayer(g);
        DrawInformation(g);
    }

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

    // -- Dispose the bitmap.
    _Bitmap.Dispose();

    // -- Flush.
    e.Graphics.Flush();

    // -- Variables.
    GC.Collect();
    GC.WaitForPendingFinalizers();
}


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
Posted
Updated 19-Dec-12 7:38am
v2
Comments
Yvar Birx 19-Dec-12 14:39pm    
Someone please help.
Mike Hankey 19-Dec-12 14:42pm    
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)

  Print Answers RSS


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