有时我的代码会给我“内存不足异常”,它会在执行我的并行循环 5 或 10 分钟后随机引发。File.ReadAllLines(line)抛出异常当我禁用缓存计时器时,一切正常。我有 2 个 190MB 的文件,每 5 秒读取一次。代码部分形成我的应用程序:
class MC {
private readonly ConcurrentDictionary<string, Lazy<string[]>> _files = new ConcurrentDictionary<string, Lazy<string[]>>();
private readonly Timer _cache = new Timer();
private MC ()
{
_cache.Enabled = true;
_cache.Interval = (int) Settings.Default.fileCache*1000; //seconds
_cache.Elapsed += (sender, args) => _files.Clear();
_cache.Start();
Parallel.ForEach(lines, options, (line, loopState, idx) =>
{
//code
string[] resultStrings = {};
var fileKey = GetMD5Hash(line);
resultStrings = _files.GetOrAdd(fileKey, new Lazy<string[]>(() => File.ReadAllLines(line),
LazyThreadSafetyMode.ExecutionAndPublication)).Value;
Fun(string.Join("", resultStrings.OrderBy(x => GetThreadRandom().Next()).Take(10))); //code
}
}
}
来自并行 lopp 的行看起来像(它有 100 000 000 条记录):
c:\a.txt
c:\b.txt
c:\a.txt
c:\b.txt
c:\a.txt
c:\b.txt
c:\a.txt
c:\b.txt
c:\a.txt
c:\b.txt
c:\a.txt
c:\b.txt