简体   繁体   English

使用大列表时出现System.OutOfMemoryException

[英]System.OutOfMemoryException while working with large Lists

I have a code: 我有一个代码:

this.weights_StoA = new List<List<double>>();

if (NETWORK_MODE == 0)
{
    Random rand = new Random();

    int count = enters.Count;

    Parallel.For(0,  HIDDEN_NEURONS_COUNT, (i, loopState) =>
    {
        List<double> weights = new List<double>();

        for (int j = 0; j < count; j++)
        {
            weights.Add(rand.NextDouble());
        }

        lock (weights_StoA)
        {
            weights_StoA.Add(weights);
        }
    });
}

weights_StoA is a List<List<double>> . weights_StoA是一个List<List<double>>

I working with large arrays. 我正在处理大型阵列。 HIDDEN_NEURONS_COUNT = 63480, entres.Conut = 126960 . HIDDEN_NEURONS_COUNT = 63480, entres.Conut = 126960 This code throws System.OutOfMemoryException . 此代码引发System.OutOfMemoryException I tried to change architecture to x64 but it still throws the same exception. 我试图将体系结构更改为x64,但是它仍然抛出相同的异常。

How do I can fix this? 我该如何解决? I will be very grateful if you help me to solve this problem! 如果您能帮助我解决这个问题,我将不胜感激!

Disregarding the fact your program needs over 100GB of RAM to operate, if you know the size of a list beforehand then either preallocate it or use a fixed-size array: this avoids dynamic resizing and reallocations: 忽略程序需要超过100GB RAM的事实,如果您事先知道列表的大小,则可以预先分配它或使用固定大小的数组:这避免了动态调整大小和重新分配:

List<double> weights = new List<double>( count );
for( int j = 0; j < count; j++ )
{
     weights.Add( rand.NextDouble() );
}

or: 要么:

double[] weights = new double[count];
for( int j = 0; j < count; j++ )
{
     weights[j] = rand.NextDouble();
}

The .Net garbagge collector does not compacts large objects to avoid performace impact. .Net垃圾收集器不会压缩大型对象,以免影响性能。 Thus, you have 2 options: 因此,您有2个选择:

  1. Allocate once the array for large data. 为大型数据分配一次阵列。

  2. Periodically set the value of the property GCSettings.LargeObjectHeapCompactionMode to GCLargeObjectHeapCompactionMode.CompactOnce . 定期将属性GCSettings.LargeObjectHeapCompactionMode的值设置为GCLargeObjectHeapCompactionMode.CompactOnce The next GC invocation will deal with the large objects, and the will reset to default value. 下一个GC调用将处理大对象,并且将重置为默认值。 See https://msdn.microsoft.com/en-us/library/system.runtime.gcsettings.largeobjectheapcompactionmode(v=vs.110).aspx 参见https://msdn.microsoft.com/zh-cn/library/system.runtime.gcsettings.largeobjectheapcompactionmode(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM