4

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?

Jason Higgins
  • 1,516
  • 1
  • 17
  • 37
Saul
  • 17,973
  • 8
  • 64
  • 88
  • 5
    Well, you know, it may be out of memory – sehe Dec 05 '12 at 16:19
  • Other data structures such as List dynamically allocate memory, and whilst this may have some performance disadvantages, you avoid running out of memory upon declaration. Is there a reason for declaring such a massive array? (I should probably say 'dynamically resize' rather than allocate) – Charleh Dec 05 '12 at 16:25
  • @sehe - Memory is available allright but turns out there is a hardcoded [2GB limit to object size](http://stackoverflow.com/a/13728766/426379). – Saul Dec 05 '12 at 17:34

6 Answers6

8

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

Community
  • 1
  • 1
Pete Garafano
  • 4,863
  • 1
  • 23
  • 40
  • 1
    Almost but not quite; the issue boiled down to the [2GB limit for any **object** inside a managed .NET application](http://stackoverflow.com/a/13728766/426379). – Saul Dec 05 '12 at 17:22
6

Besides obvious 'out of memory' semantics, there is the slightly more subtle issue of Heap fragmentation: there might be more than 2Gb or RAM available, but it might not be contiguous.

This is known as fragmentation. There is a heap profiler for dotNET that can show you when this is the case.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    [Eric Lippert put it this way](http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx) *Rather an "out of memory” error happens because the process is unable to find a large enough section of contiguous unused pages in its virtual address space to do the requested mapping.* – Conrad Frix Dec 05 '12 at 17:19
1

On a standard 32 bit system this would not be possible due to the RAM size. You would overflow the memory. On a 64 bit system this is possible because you have more address space, but is still not recommended because you would want to support both 32 and 64 bit systems with any standard application you would be making.

Jason Higgins
  • 1,516
  • 1
  • 17
  • 37
  • You could still overflow RAM on a 64bit machine with this code. – Pete Garafano Dec 05 '12 at 16:20
  • It's still definitely possible to overflow! I was just addressing the fact that 64 bit systems have a larger address space to encompass a request like this. This is still something that should NEVER be done. – Jason Higgins Dec 05 '12 at 16:22
  • @TheGreatCO A 32 bit system *will* error when doing this, a 64 bit system *might* error when doing this. – Servy Dec 05 '12 at 16:31
  • Yeah, creating an array that way is a waste of memory unless you are filling it all the way. – Pete Garafano Dec 05 '12 at 16:31
1

The problem might be not that you don't have the memory "available", but that you've fragmented the memory so much that when you try to create array, and it must be resized, no single block of available memory can hold it.

Tomek
  • 3,267
  • 2
  • 22
  • 23
1

Int32.MaxValue = 2 147 483 647 bytes = 2048 megabytes

See this link

In "Memory and Address Space Limits" see "User-mode virtual address space for each 32-bit process" and "User-mode virtual address space for each 64-bit process". So it does not seem a OS limit.

Please see this link

Community
  • 1
  • 1
dperez
  • 592
  • 5
  • 11
1

Turns out this happens because there is a hardcoded memory limit for any object created inside a managed .NET application:

When you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB).

 

See also

Community
  • 1
  • 1
Saul
  • 17,973
  • 8
  • 64
  • 88