使用ConcurrentDictionary GetOrAdd随机出现内存不足异常

时间:2013-09-19 13:44:31

标签: c# multithreading

有时我的代码会给我“Out of memory exception”,它会在执行并行循环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
                                                    }

            }

    }
来自parallel lopp的

看起来像(它有1亿条记录):

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

0 个答案:

没有答案