帮我分析下,下面这段代码为啥报内存不足??

gzl1990 2009-03-29 03:11:09
for (int i = 0; i < 100; i++)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.jubaoxiang.com/checkcode.aspx");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
Bitmap bmp = new Bitmap(stream);
setNoBorder(bmp);
int l = GetL(bmp, 0);
int r = GetR(bmp, l);
int t = GetT(bmp, l, r);
int b = GetD(bmp, l, r, t);
int l2 = GetL(bmp, r);
int r2 = GetR(bmp, l2);
int t2 = GetT(bmp, l2, r2);
int b2 = GetD(bmp, l2, r2, t2);
int l3 = GetL(bmp, r2);
int r3 = GetR(bmp, l3);
int t3 = GetT(bmp, l3, r3);
int b3 = GetD(bmp, l3, r3, t3);
int l4 = GetL(bmp, r3);
int r4 = GetR(bmp, l4);
int t4 = GetT(bmp, l4, r4);
int b4 = GetD(bmp, l4, r4, t4);
Rectangle e1 = new Rectangle(l, t, r - l, b - t);
Rectangle e2 = new Rectangle(l2, t2, r2 - l2, b2 - t2);
Rectangle e3 = new Rectangle(l3, t3, r3 - l3, b3 - t3);
Rectangle e4 = new Rectangle(l4, t4, r4 - l4, b4 - t4);
Bitmap bit = bmp.Clone(e1, bmp.PixelFormat);
Bitmap bit2 = bmp.Clone(e2, bmp.PixelFormat);
Bitmap bit3 = bmp.Clone(e3, bmp.PixelFormat);
Bitmap bit4 = bmp.Clone(e4, bmp.PixelFormat);//这里以及上面上行都有可能报内存不足
pictureBox1.Image = bit;
pictureBox2.Image = bit2;
pictureBox3.Image = bit3;
pictureBox4.Image = bit4;
bit.Save("F:\\Images\\str" + i + ".bmp");
bit.Dispose();
bit2.Dispose();
bit3.Dispose();
bit4.Dispose();
bmp.Dispose();
}
...全文
170 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgke 2009-03-30
  • 打赏
  • 举报
回复
你好象在分割图片...你的GetL这些方法是做什么的..检查下里面的方法 Graphics 需要释放的.
junyi2003 2009-03-30
  • 打赏
  • 举报
回复
好像是做验证码吧?不清楚,感觉是对象/内存还没被释放 就又使用了。导致内存泄露

貌似代码不够合理,暂时看不出头绪
无人境域 2009-03-30
  • 打赏
  • 举报
回复
看不到完整代码,只好猜了。
递归调用了,而且没有边界。
cuike519 2009-03-30
  • 打赏
  • 举报
回复
                pictureBox1.Image = bit; 
pictureBox2.Image = bit2;
pictureBox3.Image = bit3;
pictureBox4.Image = bit4;
bit.Save("F:\\Images\\str" + i + ".bmp");
bit.Dispose();
bit2.Dispose();
bit3.Dispose();
bit4.Dispose();
bmp.Dispose();


首先,你的for循环创建了很多Bitmap对象,这个对象本身比较占内存
其次,上面这段代码,你认识Dispose方法会释放这些对象么?实际上这些对象是不会被释放的,因为他们和pictureBox关联,如果你能dump下来内存的话问题就很容易分析了,目前我也只是猜测,你创建的所有Bitmap对象都是强引用到PictureBox对象,这个对象又关联到Form对象,这种对象是不会当垃圾回收的。
最后,你可以适当的减少循环,优化算法。

另外:如果你有adplus工具,可以dump该进程的内存,使用windbg+sos查看内存的使用情况,找到被Hold的内存究竟是被谁Hold住了。

由于没有你的内存环境,上面的结论只是猜测,仅供参考。
gzl1990 2009-03-30
  • 打赏
  • 举报
回复
我像那样写行申明(在方法外),用完以后再把它们置为null或者0;i可以到达60,然后报内存不足异常了
gzl1990 2009-03-30
  • 打赏
  • 举报
回复
HttpWebRequest request = null;
HttpWebResponse response = null;
Bitmap bmp = null;
Bitmap bit = null;
Bitmap bit2 = null;
Rectangle e1 = new Rectangle();
Rectangle e2 = new Rectangle();
int l = 0;
int r = 0;
int t = 0;
int b = 0;
int l2 = 0;
int r2 = 0;
int t2 = 0;
int b2 = 0;
private void btnGetBmp_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 100; i++)
{
request = (HttpWebRequest)WebRequest.Create("http://www.jubaoxiang.com/checkcode.aspx");
response = (HttpWebResponse)request.GetResponse();
bmp = new Bitmap(response.GetResponseStream());
setNoBorder(bmp);
l = GetL(bmp, 0);
r = GetR(bmp, l);
t = GetT(bmp, l, r);
b = GetD(bmp, l, r, t);
l2 = GetL(bmp, r);
r2 = GetR(bmp, l2);
t2 = GetT(bmp, l2, r2);
b2 = GetD(bmp, l2, r2, t2);

e1 = new Rectangle(l, t, r - l, b - t);
e2 = new Rectangle(l2, t2, r2 - l2, b2 - t2);

bit = bmp.Clone(e1, bmp.PixelFormat);
bit2 = bmp.Clone(e2, bmp.PixelFormat);


//Str str = GetAllXY(bit);
//Str str2 = GetAllXY(map);


pictureBox1.Image = bit2;
txtStr.Text = this.checkCode;
bmp.Dispose();
request = null;
response = null;
bit.Dispose();
bit2.Dispose();
l = 0;
r = 0;
t = 0;
b = 0;
l2 = 0;
r2 = 0;
t2 = 0;
b2 = 0;

}
「已注销」 2009-03-30
  • 打赏
  • 举报
回复
看着就要报内存不足,前面占用的内存没有释放,程序不合理。
Teng_s2000 2009-03-30
  • 打赏
  • 举报
回复
Clone干什么呢
gzl1990 2009-03-30
  • 打赏
  • 举报
回复
那只是请求
没关系的吧
zw_l_1989 2009-03-30
  • 打赏
  • 举报
回复
打开100个http://www.jubaoxiang.com/checkcode.aspx?
gzl1990 2009-03-30
  • 打赏
  • 举报
回复
对图片处,当i=6的时候就会有报内存不足,我想可能要清理点资源吧,但不知道怎么清,dispose()试过了没用,让他成null也没用样

111,126

社区成员

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

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

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