- 内存不足?代码错误?都不是
昨天写一个处理从DB 中导出图片的东西,原理很简单,就是将数据库中的二进制导出为图片(JPG),但是数据量非常大(超过10万行数据,DB超过80GB),代码很快写完,但是在测试过程中,有图片因为内存不足而导出失败,并且大约执行到2000条左右时,因内存不足而终止执行,最开始没有发现,想的是毕竟数据量大,内存不足正常,还想到了直接将执行失败的数据标记出来,另外再执行(点开软件,接着没执行完的继续执行),至于因内存不足导出失败的记录,开始的想法是再加一个功能按钮,毕竟执行失败的几率已经用一个文本单独记录了,添加进来继续执行呗。然而今早过来,习惯性的重新读一遍自己的代码的时候,发现逻辑上,总体没问题,然而,当看到数据库打开(代码中“标记1”)后,关闭是在代码执行完(代码中“//标记5”),或许是早上脑袋还清醒,突然反应过来,数据库打开后,没有及时关闭,导致内存不断上涨,后面试了,“打开使用后及时关闭”(增加了代码中的“标记2”和“标记3”,一关闭一打开。同时“标记5”关闭相应的移动到“标记4”位置),一测试,10万+的记录顺利执行完毕,内存一直维持在20%(14G)左右。
搞不懂的是,为什么内存超过 25%就会直接中断?BUG?求教
下附部分代码
string txtPath =System.Windows.Forms.Application.StartupPath + "\\workState.txt";
File.AppendAllText(txtPath, "**********开始执行:******************" + "\n");
string errPath = System.Windows.Forms.Application.StartupPath + "\\Err.txt";
File.AppendAllText(errPath, "**********开始执行:******************" + "\n");
byte[] imagebytes = null;
int k =Convert.ToInt32(textBox3.Text);
int s = Convert.ToInt32(textBox4.Text);
waitor.Text = "读取数据";
string dbPath = @"Data Source=" + textBox1.Text + ";Pooling=true;FailIfMissing=false";
dbCon = new SQLiteConnection(dbPath);
if (dbCon.State != ConnectionState.Open)
{
//标记1
dbCon.Open();
int totalRecord = 0;
string sqlQuryTotal = "SELECT COUNT(*) FJ FROM FJ";
SQLiteCommand cmd = new SQLiteCommand(sqlQuryTotal, dbCon);
SQLiteDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
waitor.Text = (string)dr.GetValue(0).ToString();
totalRecord = Convert.ToInt32(waitor.Text);
}
//标记2
dbCon.Close();
tspb.Maximum = totalRecord;
for (int j = s; j <= totalRecord; j = j + k)
{
//标记3
dbCon.Open();
tspb.Value = j;
string sqlstr = "SELECT FJMC,FJ FROM FJ ORDER BY FJMC LIMIT " + j + "," + k;
SQLiteDataAdapter da = null;
da = new SQLiteDataAdapter(sqlstr, dbCon);
DataTable dt = null;
dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
string fjmc = dt.Rows[i][0].ToString();
string fdNam = fjmc.Split('_')[0];
string imgNam = fjmc.Split('_')[1];
string fdOut = textBox2.Text + "\\" + fdNam;
string imgSavPath = fdOut + "\\" + imgNam;
string endX = imgNam.Split('.')[1];
if (endX == "jpg")
{
if (!Directory.Exists(fdOut))
{
Directory.CreateDirectory(fdOut);
}
string sqlQuryImg = "SELECT FJ FROM FJ WHERE FJMC = '" + fjmc + "'";
SQLiteCommand cmd1 = new SQLiteCommand(sqlQuryImg, dbCon);
SQLiteDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
try
{
imagebytes = (byte[])dr1.GetValue(0);
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bitmap = new Bitmap(ms);
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image.Save(imgSavPath);
pictureBox1.Image = null;
ms.SetLength(0);
}
catch (Exception ex)
{
File.AppendAllText(errPath, "附件名称[" + fjmc + "]解译失败,已跳过,原因为:" + ex.Message + "\n");
}
}
}
else
{
File.AppendAllText(errPath, "附件名称[" + fjmc + "]非JPG照片格式,已跳过,格式为[" + endX + "]" + "\n");
}
}
File.AppendAllText(txtPath, textBox1.Text + "," + textBox2.Text + "," + j.ToString() + "\n");
dt.Clear();
dt = null;
da = null;
//标记4
dbCon.Close();
}
//标记5
}