-2

I tried this using VS 2013. I got an exception saying "An unhandled exception of type 'System.OutOfMemoryException' occurred in byteArray.exe" (I used byte instead of int) . .

static void Main(string[] args)
  {
  byte[,] a = new byte[100000, 100000];
  for(int i=0;i<100000;i++)
        {
            for(int j=0;j<100000; i++)
            {
                Console.Write(0+" ");
            }
        }
    }

enter image description here

10
  • As it said. You don't have enough memory to store such a big array like that.
    – Envil
    Jul 25, 2015 at 3:06
  • Do you really have free 10GB RAM for this task?
    – user5147454
    Jul 25, 2015 at 3:10
  • No.. I have just 3GB RAM (Total). Does it need 10GB ? Jul 25, 2015 at 3:14
  • @VikasThakur you should calculate how much your program will ate RAM. (100'000 ^ 2) / (1'024 ^ 3) in GB.
    – user5147454
    Jul 25, 2015 at 3:19
  • I understand now. Thank you very much Jul 25, 2015 at 3:21

2 Answers 2

2

100,000 times 100,000 bytes is going to need about 10GB of space, it's unlikely you'll be able to allocate that in a 32bit OS, where you're limited to a 4G address space (and there are other things that will be using it as well).

Even if your heap could handle that (and it may be possible in a 64bit OS), you should think about it very carefully.

Instead, you may want to think about other options such as disk storage, bringing in only what's needed as you need it. This could be as simple as memory-mapping a 10G file and letting the OS handle paging of it to and from disk as needed.

Or, depending on your actual needs (rather than a specific implementation to meet those needs), there may be other ways.

1

If using visual studios with .net 4.5 or higher. You can set your program build platform target to x64.

Then include the below in your App.Config file.

<runtime>
    <gcAllowVeryLargeObjects enabled="true"/>
</runtime>

The above will let you use a jagged array like this and yes it will use 10gb of memory.

        byte[][] array = new byte[100000][];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = new byte[100000];
        }

Edit: As said by paxdiablo this should not be used as there should be a better way to accomplish what you are trying. Also you need a 64 bit OS to be able to run the above.

3
  • @paxdiablo Yes i figured that was obvious by x64 platform target. But you are right i should have stated that above. I added it above thanks pax. Jul 25, 2015 at 3:42
  • allocating each row separately like that is very bad in terms of locality, and it's also slower than a single 2 dimentional array
    – phuclv
    Jul 25, 2015 at 3:50
  • @LưuVĩnhPhúc what makes you think that allocating sequence of arrays will lead to significantly different memory layout (and hence locality) than one long array? Jul 25, 2015 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.