Create a huge dictionary

In my search for primes, I already asked this question: It is impossible to create huge arrays that will lead me to create my own class of fake arrays based on the array dictionary ...: private Dictionary<int, Array> arrays = new Dictionary<int, Array>();

I know that I am creating fake arrays of a large number of bool (e.g. 10 000 000 000 ) using the following code:

 public class CustomArray { private Dictionary<int, Array> arrays = new Dictionary<int, Array>(); public CustomArray(ulong lenght) { int i = 0; while (lenght > 0x7FFFFFC7) { lenght -= 0x7FFFFFC7; arrays[i] = new bool[0x7FFFFFC7]; i++; } arrays[i] = new bool[lenght]; } } 

But it crashes as soon as I request a CustomArray of elements of 100 000 000 000 . It works well for the first 25 iterations (my dictionary contains 25 arrays of elements 0x7FFFFFC7), but then it crashes with OutOfMemory exception.

Otherwise, I have 16 gigabyte memory, VS2013, the program is compiled into 64 bits, I turned on the gcAllowVeryLargeObjects parameter, and I do not see the memory peak in the task manager.


How to avoid this error?

+5
source share
1 answer

100000000000 bools means ~ 93 GB of memory. You only have 50 GB (including the default virtual memory allocated).

Saving them as bits (not as bytes) will lead you to ~ 12 GB.

Take a look at System.Collection.BitArray

+7
source

All Articles