Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C#
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:
 
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 19-Dec-12 8:37am
Yvar Birx2.4K
Edited 19-Dec-12 8:38am
v2
Comments
Yvar Birx at 19-Dec-12 14:39pm
   
Someone please help.
Mike Hankey at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 295
1 OriginalGriff 274
2 Shweta N Mishra 216
3 PIEBALDconsult 184
4 CPallini 155
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100