用C#处理大图像,内存占用十分惊人,该如果解决?

haiwangstar 2004-04-06 05:08:24
程序中用一个循环对某文件夹下所有图像文件做处理,图像本身比较大(2848*4256),图像文件本身占用内存很大,而且在.NET中分配的内存不能像C++那样手动立即释放,GC的垃圾收集所释放的内存,抵不上程序处理所占用的内存.导致很快就会出现内存不够用的现象,实在想不出什么解决方案,不知大家有没有好的方法..谢谢!!!!!!

for(int i=0; i<= n; i++)
{

TransSingleImage(...);

}


private void TransSingleImage(string str)
{
Bitmap bit = null;
Bitmap bitnew = null;
Graphics g = null;
Image img = null;
string filename;

try
{
img = Image.FromFile(str);
filename = Path.GetFileName(str);

}
catch
{
return;
}

...

bitnew.Save(Path.Combine(dc,file) , f);
g.Dispose();
bit = null;
bitnew = null;
img = null;


catch(Exception ex)
{
MessageBox.Show("保存文件时出错");
}
}




...全文
1295 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wq_sc 2004-04-06
  • 打赏
  • 举报
回复
up
我不懂电脑 2004-04-06
  • 打赏
  • 举报
回复
组件都有Dispose()方法由客户调用手工释放内存。
tangligang 2004-04-06
  • 打赏
  • 举报
回复
Bitmap bit = null;
Bitmap bitnew = null;
Graphics g = null;
Image img = null;

for(int i=0; i<= n; i++)
{

TransSingleImage(...);

}


private void TransSingleImage(string str)
{

string filename;

try
{
img = Image.FromFile(str);
filename = Path.GetFileName(str);

}
catch
{
return;
}

...

bitnew.Save(Path.Combine(dc,file) , f);


catch(Exception ex)
{
MessageBox.Show("保存文件时出错");
}
}

g.Dispose();
bit = null;
bitnew = null;
img = null;

GC.Collect (0);
hertcloud 2004-04-06
  • 打赏
  • 举报
回复
使用 对象的 Dispose()方法
自己 释放 没有用 资源
gOODiDEA 2004-04-06
  • 打赏
  • 举报
回复
没测试过,试试:


Bitmap bit = null;
Bitmap bitnew = null;
Graphics g = null;
Image img = null;

for(int i=0; i<= n; i++)
{

TransSingleImage(...);

}


private void TransSingleImage(string str)
{

string filename;

try
{
img = Image.FromFile(str);
filename = Path.GetFileName(str);

}
catch
{
return;
}

...

bitnew.Save(Path.Combine(dc,file) , f);


catch(Exception ex)
{
MessageBox.Show("保存文件时出错");
}
}

g.Dispose();
bit = null;
bitnew = null;
img = null;
haiwangstar 2004-04-06
  • 打赏
  • 举报
回复
g.Dispose();
bit.Dispose();
bitnew.Dispose();
img.Dispose();

GC.Collect();

能用的方法都用.这样做了以后,从任务管理器上看,每一个循环过后,内存占用马上降下来了,效果还是令人满意的,要知道我的每张图片,如果是BMP格式,一张就40多M.

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧