List的内存浪费问题? [英] Memory wasting problem with List?

查看:90
本文介绍了List的内存浪费问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List< bitmap>,我尝试在该列表中添加大约60个Bitmap对象,并打开任务管理器以查看它使用了多少内存,之后我按下了一些按钮来调用Remove(),RemoveRange,..列表的方法<位图>并查看使用的内存是否减少(释放),但我没有看到我的应用程序使用的内存有任何变化。



你能给我一个解决方案吗?我已经尝试定期使用System.GC.Collect()(周期约为5秒),似乎工作正常,但有些人说这不好。我不知道为什么,因为它确实有助于我释放未使用的内存,如果不好的话,我想知道另一个解决方案。



你的帮助将是非常感谢!

谢谢。



VipHaLong

解决方案

它''不要自己调用GC - 如果遵守规则则不需要。

在这种情况下,您要添加位图 - 它们是大对象并且支持Dispose方法。

因此,当您从列表中删除它们并停止需要它们时,请将它们删除。

 私有 无效 删除(列表<位图>列表, int  index)
{
if (list.Count > index)
{
Image i = list [index];
list.RemoveAt(index);
i.Dispose();
}
}

然而,这不会减少任务管理器中显示的内存 - 一旦.NET扩展堆,它就不会减少它。


I have a List<bitmap>, I tried adding to that list about 60 Bitmap objects, and opened Task Manager to see how much memory it used, after I pressed on some button to call Remove(), RemoveRange, ... methods of the List<bitmap> and see if the used memory was decreased (freed), however I didn''t see any change in memory used by my application.

Could you please give me a solution for this? I have tried using System.GC.Collect() periodically (with a cycle of about 5 seconds), it seems to work OK, but some people say it''s not good. I don''t know why, because it really helps me free unused memory, I would like to know another solution if that is not good.

Your help would be highly appreciated!
Thanks.

VipHaLong

解决方案

It''s not good to call the GC yourself - you shouldn''t need to if you obey the rules.
In this case, you are adding Bitmaps - which are large objects and which support the Dispose method.
So when you remove them from your list and stop needing them, get rid of them then.

private void remove(List<Bitmap> list, int index)
    {
    if (list.Count > index)
        {
        Image i = list[index];
        list.RemoveAt(index);
        i.Dispose();
        }
    }

However that won''t reduce the memory shown in the Task Manager - once .NET expands the heap, it doesn''t reduce it.


这篇关于List的内存浪费问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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