4

I need to make an 2d array, but c# won't let me and says that it is too big, any ideas how to make it work?

int[,] arrayName = new int[37153,18366];
sventevit
  • 4,766
  • 10
  • 57
  • 89
  • 4
    This array need 2.6GB memory (37153 x 18366 x 4 Bytes) – Sir Rufo Dec 26 '15 at 08:41
  • Why do you need such a big array in memory? Can't you use a database instead and get only useful data in memory? – Shaharyar Dec 26 '15 at 08:42
  • Take a look at [What is the maximum length of an array in .NET on 64-bit Windows](http://stackoverflow.com/questions/2338778/what-is-the-maximum-length-of-an-array-in-net-on-64-bit-windows) – dbc Dec 26 '15 at 08:43
  • If you are running on a 64 bit machine and using .Net 4.5 or later, you could try setting [``](https://msdn.microsoft.com/en-us/library/hh285054%28v=vs.110%29.aspx) as is described [here](https://stackoverflow.com/questions/2338778/what-is-the-maximum-length-of-an-array-in-net-on-64-bit-windows). – dbc Dec 26 '15 at 09:26

2 Answers2

3

The max theoretical size of an int array is 2147483647 i.e. int[] array = new int[2147483647] but the probleming you're having here is that the computer runs of out memory.

Read this for explanation and tips on how to solve this: OutOfMemoryException on declaration of Large Array

Community
  • 1
  • 1
Westerlund.io
  • 2,743
  • 5
  • 30
  • 37
1

If you are not using the complete range of the array (which is in your case 2,7GB of RAM), you could use a Dictionary.

https://msdn.microsoft.com/de-de/library/xfhwa508(v=vs.110).aspx

Alternative: Create a [][] Array. In this case you must initialize each row. But you can access each cell easy with arrayName[row][col].

    int[][] arrayName = new int[37153][];
    for(int i=0; i<arrayName.Length; i++)
        arrayName[i] = new int[18366];
thomas
  • 330
  • 3
  • 10