OutOfMemoryException on declaration of Large Array

前端 未结 4 1516
渐次进展
渐次进展 2020-11-29 07:41

I have to create a fairly large double array 12000ish x 55000ish. Unfortunately, I get an out of memory exception. I used to develop in Java and could change the memory sett

4条回答
  •  借酒劲吻你
    2020-11-29 08:17

    Since you can't create objects larger than 2GB you can try to use MemoryMappedFile to work with chunk of memory of the required size.

    
    var data = MemoryMappedFile.CreateNew("big data", 12000L * 55000L);
    var view = data.CreateViewAccessor();
    var rnd = new Random();
    
    for (var i = 0L; i < 12000L; ++i)
    {
        for (var j = 0L; j < 55000L; ++j)
        {
            var input = rnd.NextDouble();
            view.Write(i * 55000L + j, ref input);
        }
    }
    
    

提交回复
热议问题