[C#/绘图]内存问题 - 内存不断上升 [英] [C#/Drawing] Memory Issue - Memory Keeps Rising

查看:319
本文介绍了[C#/绘图]内存问题 - 内存不断上升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的CodeProject会员,



我一直面临严重的记忆问题,他们正在推动我的努力。目前我正在制作一款像野味一样的游戏,你可以在这个视频中看到:

https ://www.youtube.com/watch?v = bQ_9m-KkdBQ [ ^ ]



我已经能够制作一个完全正常工作的块系统,到目前为止我已经更新了它以便我可以有大约250种不同的块。虽然这不是我的问题所在,但我似乎只是在获得记忆。它让我疯狂,我不知道为什么。这就是我的意思:



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



我尝试仅使用Paint事件本身的图形进行绘制,但这样的结果与更多内存相同。这是正常还是我严重搞砸了什么?



这是我的一段代码:



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();
}





有趣的是,即使没有走出第一块,问题仍然会发生,所以这不是一个块错误。我已经检查了100多次。



我还要补充说这个记忆人们告诉我,过了一段时间后它没有下降到它的原始数量。



任何人?救命 ! :L



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

推荐答案

首先,不要看任务管理器告诉你应用程序使用了多少内存。这对你说谎。它显示了.NET CLR保留了多少内存,而不是你的应用实际使用了多少。



摆脱GC的东西,你不要不需要它。您实际上也不需要绘制到Bitmap对象。你所要做的就是将你从事件args获得的Graphics对象传递给你的DrawXxx方法。



如果你想看看你的应用实际使用的是什么,使用PerfMon和.NET内存计数器。
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.


这篇关于[C#/绘图]内存问题 - 内存不断上升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆