是不是我的vs2012设置哪里不对啊,最近学习Parallel,都是照着实例运行,几乎半数以上的程序会报错中断,而直接到debug目录下运行exe文件又可以正常执行。如下代码
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Collections.Generic;using System.Linq;class Program
{
    static void Main(string[] args)
    {
        var dic = LoadData();        Stopwatch watch = new Stopwatch();        watch.Start();        //串行执行
        var query1 = (from n in dic.Values
                      where n.Age > 20 && n.Age < 25
                      select n).ToList();        watch.Stop();        Console.WriteLine("串行计算耗费时间:{0}", watch.ElapsedMilliseconds);        watch.Restart();        var query2 = (from n in dic.Values.AsParallel()
                      where n.Age > 20 && n.Age < 25
                      select n).ToList();
//这句报错“System.OutOfMemoryException”类型的未经处理的异常在 mscorlib.dll 中发生        watch.Stop();        Console.WriteLine("并行计算耗费时间:{0}", watch.ElapsedMilliseconds);        Console.Read();
    }    public static ConcurrentDictionary<int, Student> LoadData()
    {
        ConcurrentDictionary<int, Student> dic = new ConcurrentDictionary<int, Student>();        //预加载1500w条记录
        Parallel.For(0, 15000000, (i) =>
        {
            var single = new Student()
            {
                ID = i,
                Name = "hxc" + i,
                Age = i % 151,
                CreateTime = DateTime.Now.AddSeconds(i)
            };
            dic.TryAdd(i, single);
        });        return dic;
    }    public class Student
    {
        public int ID { get; set; }        public string Name { get; set; }        public int Age { get; set; }        public DateTime CreateTime { get; set; }
    }
}var query2 = (from n in dic.Values.AsParallel()
                      where n.Age > 20 && n.Age < 25
                      select n).ToList();
这句报错为:““System.OutOfMemoryException”类型的未经处理的异常在 mscorlib.dll 中发生”

解决方案 »

  1.   

    //预加载1500w条记录        
    Parallel.For(0, 15000000, (i) =>        
    {  
      int v = i;
      var single = new Student()         
       {  
         ID = v,              
         Name = "hxc" + v,             
         Age = v % 151,         
         CreateTime = DateTime.Now.AddSeconds(v)          
      };       
         dic.TryAdd(v, single);      
      });System.OutOfMemoryException 内存溢出。
    出错并不是下面代码的原因,
     var query2 = (from n in dic.Values.AsParallel()                  
        where n.Age > 20 && n.Age < 25                     
        select n).ToList();而是dic已有1500w条记录,在未释放dic下,再使用AsParallel 则需要新的集合内存,其他运算也需要内存,
    所以就可能导致内存不足而引发 System.OutOfMemoryException 异常
      

  2.   


    不好意思,我不大理解你的意思。这段代码是网上复制的,那个作者运行却正常。我编译后直接运行exe也是可以正常运行的,但debug模式下会在那句中断,如果我点继续运行,他又可以运行下去。但每次debug都会中断在那里,且提示那个错误信息。
      

  3.   

    调试的时候有调试信息,要跟踪到每个线程,因此消耗的内存资源远大于直接运行,而不是1楼所说的dic的释放,毫无关系。说出那样的话,说明他还不理解class和struct的区别,这里面不管存在多少个副本,每个对象都只有4字节的副本开销(引用地址)。但是调试信息的跟踪记录开销就不小了,VisualStudio会将执行过程全部记录到内存,你1500W的记录,会产生多少记录?
      

  4.   


    谢谢,为什么会导致内存不足?我电脑内存是4g。不知道我这样说对不对。。
    请问那代码应该如何修改呢,谢谢VS2010很耗内存,不知道VS2012的情况,我这样说不知道对不对?
      

  5.   


    谢谢,为什么会导致内存不足?我电脑内存是4g。不知道我这样说对不对。。
    请问那代码应该如何修改呢,谢谢
    4G那要看你的操作系统是不是64的。
    32位的进程所用的内存都有一定的限额的。
    64会高一些我是win 7 64位的系统