c#GDI +,在循环中创建LinearGradientBrush(内存泄漏) [英] c# GDI+, Creating a LinearGradientBrush in a loop (memory leaks)

查看:127
本文介绍了c#GDI +,在循环中创建LinearGradientBrush(内存泄漏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我遇到了一些难题.我创建了一个使用GDI +绘制表格的应用程序.每秒由计时器触发一次绘图. draw方法使用for循环遍历对象集合,如果它们处于特定状态,则绘制它们.

Today I came across a bit of a dilema. I have created an app that uses GDI+ to draw on a form. The drawing is triggered by a timer every second. The draw method uses a for loop to iterate through a collection of objects and if they are of a certain state, draw them.

我想使用LinearGradientBrush绘制它们只是因为它看起来比简单的Brush好很多.看看下面的

I want to draw them using a LinearGradientBrush simply because it looks so much nicer than a simple Brush. Have a look at the following

            //minutes
        foreach (Led l in MinuteGrid.Leds)
        {
            LinearGradientBrush b = new LinearGradientBrush
                (l.LedRectangle, Color.GreenYellow, Color.Green, 110);

            if (l.IsLit)
                g.FillRectangle(b, l.LedRectangle);

            b.Dispose();
        }

我为循环的每次迭代创建了一个新的LinearGradientBrush(这让我感到困扰),但这就是因为我必须这样做.我无法在循环外创建一个,因为其构造函数集要求我设置仅在循环内已知的参数.

I am creating a new LinearGradientBrush for each iteration of the loop (which bothers me), but thats because I have to. I cannot create one outside the loop because its constructor set demands that I set parameters which are only ever known inside the loop.

我发现在LinearGradientBrush对象上使用dispose方法并不是那么可靠.如果我运行我的应用程序并在任务管理器中查看它,则会弹出内存.然后,当我添加b = null行时,看起来很有帮助,如下所示:

I find that using the dispose method on the LinearGradientBrush object is not all that reliable. If I run my app and view it in Task manager, its spewing memory. When I then add the b = null line that seems to help hugely as follows

            foreach (Led l in MinuteGrid.Leds)
        {
            LinearGradientBrush b = new LinearGradientBrush
                (l.LedRectangle, Color.GreenYellow, Color.Green, 110);

            if (l.IsLit)
                g.FillRectangle(b, l.LedRectangle);

            if (b != null)
            {
                b.Dispose();
                b = null;
            }
        }

我只是想知道是否有更好的方法来使用LinearGradientBrushes?还是有更好的解决方案?

I am just wondering if there is a better way to work with LinearGradientBrushes ? Or is there a better solution to use ?

非常感谢

推荐答案

我建议使用使用"语句:

I would recommend using a "using" statement:

foreach (Led l in MinuteGrid.Leds)
{
     if (l.IsLit)
     {
         using(LinearGradientBrush b = new LinearGradientBrush(l.LedRectangle, Color.GreenYellow, Color.Green, 110))
         {
            g.FillRectangle(b, l.LedRectangle);
         }
     }
}

但是,请记住,Dispose()不会释放(托管)内存.它仅释放非托管资源(这很重要,并且可能包括非托管内存).在GC运行之前,内存不会释放,这可能在循环期间不会发生.

However, remember, Dispose() does not free (managed) memory. It just releases the unmanaged resources (which is important, and may include unmanaged memory). The memory will not free until the GC runs, which may not happen during your loop.

但是,如果内存压力过高,则垃圾收集器应在您的循环内运行,并且您会发现它下降了. .NET是这样设计的-只需接受它,不必担心. GC最终将收集此内存,因此不必担心.

However, if the memory pressure gets too high, the garbage collector should run within your loop, and you'll see it drop. This is the way .NET is designed - just accept it, and don't worry. The GC will eventually collect this memory, so its not something to worry about.

这篇关于c#GDI +,在循环中创建LinearGradientBrush(内存泄漏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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