As you probably found out, the problem is that you are trying to allocate one large contiguous block of memory that does not work due to memory fragmentation. If I needed to do what you are doing, I would do the following:
int sizeA = 10000, sizeB = 10000; double sizeInMegabytes = (sizeA * sizeB * 8.0) / 1024.0 / 1024.0; //762 mb double[][] randomNumbers = new double[sizeA][]; for (int i = 0; i < randomNumbers.Length; i++) { randomNumbers[i] = new double[sizeB]; }
Then, to get a specific index, you must use randomNumbers[i / sizeB][i % sizeB] .
Another option, if you always refer to the values ​​in order, may be to use an overloaded constructor to specify the seed. This way you get a semi-random number (e.g. DateTime.Now.Ticks ), store it in a variable, and then when you start going through you have to create a new instance of Random using the original seed:
private static int randSeed = (int)DateTime.Now.Ticks;
It is important to note that while the blog related to Fredrik Mörk's answer indicates that the problem is due to lack of address space, he does not list a number of other problems, such as limiting the size of the CLR object to 2 GB (mentioned in a comment from ShuggyCoUk on the same blog ), ignores memory fragmentation and does not mention the effect of the page file size (and how it can be solved using CreateFileMapping ).
The 2GB limit means that randomNumbers must be less than 2 GB. Since arrays are classes and have some overhead, this means that the double array should be less than 2 ^ 31. I'm not sure how much less than 2 ^ 31 the length should have been, but the overhead of the .NET array? indicates 12 to 16 bytes.
Memory fragmentation is very similar to hard disk fragmentation. You may have 2 GB of address space, but there will be spaces between the values ​​when creating and destroying objects. If these spaces are too small for your large object, and additional space cannot be requested, you will get a System.OutOfMemoryException . For example, if you create 2 million 1024-byte objects, then you are using 1.9 GB. If you delete every object where the address is not a multiple of 3, then you will use .6 GB of memory, but it will be distributed through the address space with 2024 bytes of open blocks between them. If you need to create an object that was .2GB, you would not be able to do this because there was not a large enough block to accommodate it, and additional space could not be obtained (assuming a 32-bit environment). Possible solutions to this problem are things like using small objects, reducing the amount of data stored in memory, or using a memory management algorithm to limit / prevent memory fragmentation. It should be noted that if you are not developing a large program that uses a large amount of memory, this will not be a problem. In addition, this problem can occur in 64-bit systems, since windows are mainly limited by the page file size and the amount of RAM in the system.
Since most programs request working memory from the OS and do not request file mapping, they will be limited by system RAM and page file size. As noted in a comment by Néstor Sánchez on a blog with managed code like C #, you are stuck in limiting the RAM / page file and the address space of the operating system.
It was much longer than expected. Hope this helps someone. I published it because I encountered a System.OutOfMemoryException running an x64 program on a system with 24 GB of RAM, although there was only 2 GB in my array.