Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does creating a new array throw OutOfMemoryException?

This one throws an OutOfMemoryException.

Target framework .NET 3.5, running on a 64-bit Windows 2008 R2 Standard.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] test = new byte[Int32.MaxValue];
        }
    }
}

According to documentation, array length must simply be a positive 32-bit integer but apparently that is not the only restriction to look out for.

Why does memory run out in this case?

like image 572
Saul Avatar asked Dec 02 '22 20:12

Saul


1 Answers

That is 2 gigabytes of ram. Max value of 32 bit int is 2147483647, converted to megabytes is 2048, or 2 gigabytes. The machine may actually have run out of memory. See: Maximum Memory a .NET process can allocate

like image 152
Pete Garafano Avatar answered Dec 04 '22 09:12

Pete Garafano