我有以下代码,尝试处理400万条数据库记录:
在那段时间里,它只能通过大约35,000条记录。
private void intenseProcess4()
{
using (connection1 = new SqlConnection("connection string goes here"))
{
using (command1 = new SqlCommand(@"stored procedure name goes here", connection1))
{
command1.CommandType = CommandType.StoredProcedure;
try
{
connection1.Open();
using (reader1 = command1.ExecuteReader())
{
while (reader1.Read())
{
int PrjNameIndex1 = reader1.GetOrdinal("full_path");
Directory.CreateDirectory(Path.Combine(reader1.GetString(PrjNameIndex1)));
}
if (reader1.NextResult())
{
while (reader1.Read())
{
System.IO.File.Copy(reader1.GetString(SourceIndex), reader1.GetString(DestinationIndex), true);
}
}
}
}
catch (SqlException ex)
{
File.AppendAllText(Path.Combine(@"h:\X\log\error.log"), ex + " SqlException caught." + Environment.NewLine);
}
}
}
}
一旦启动,它可以正常运行约一个小时,但之后会出现以下错误信息:
Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: devenv.exe
Problem Signature 02: 12.0.21005.1
Problem Signature 03: 524fcb34
Problem Signature 04: mscorlib
Problem Signature 05: 4.0.30319.34209
Problem Signature 06: 534894cc
Problem Signature 07: 226e
Problem Signature 08: 6
Problem Signature 09: System.OutOfMemoryException
OS Version: 6.1.7601.2.1.0.256.49
Locale ID: 2057
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
在那段时间里,它只能通过大约35,000条记录。
String DestinationString =
和String SourceString =
是无用的。但这不应该是问题所在。 - xanatosGC.Collect()
。在while
之前:int num = 0;
在while
内部:if (++num % 1000 == 0) { GC.Collect(); }
- xanatos