【发布时间】:2020-10-09 23:40:21
【问题描述】:
我正在运行一个非常简单的函数,它从文本文件中批量读取行。每行包含一个 sql 查询,因此该函数获取指定数量的查询,针对 SQL 数据库执行它们,然后获取下一批查询,直到读取整个文件。问题是随着时间的推移,文件非常大,该过程开始显着减慢。我猜函数中某处存在内存泄漏,但无法确定它可能在哪里。此功能运行时没有其他任何事情发生。我的编程技能充其量是粗略的,所以请放轻松。 :)
for (int x = 0; x<= totalBatchesInt; x++)
{
var lines = System.IO.File.ReadLines(file).Skip(skipCount).Take(batchSize).ToArray();
string test = string.Join("\n", lines);
SqlCommand cmd = new SqlCommand(test.ToString());
try
{
var rowsEffected = qm.ExecuteNonQuery(CommandType.Text, cmd.CommandText, 6000, true);
totalRowsEffected = totalRowsEffected + rowsEffected;
globalRecordCounter = globalRecordCounter + rowsEffected;
fileRecordCounter = fileRecordCounter + rowsEffected;
skipCount = skipCount + batchSize;
TraceSource.TraceEvent(TraceEventType.Information, (int)ProcessEvents.Starting, "Rows
progress for " + folderName + "_" + fileName + " : " + fileRecordCounter.ToString() + "
of " + linesCount + " records");
}
catch (Exception esql)
{
TraceSource.TraceEvent(TraceEventType.Information, (int)ProcessEvents.Cancelling, "Error
processing file " + folderName + "_" + fileName + " : " + esql.Message.ToString() + ".
Aborting file read");
}
}
【问题讨论】:
-
sqlcommand implements iDisposable。但我怀疑这是你问题的核心。
-
它可能不是核心,但现在你指出来,它肯定是一个问题。感谢您的意见。
标签: c# sql memory-management memory-leaks