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?

3
  • 5
    Well, you know, it may be out of memory Commented Dec 5, 2012 at 16:19
  • Other data structures such as List<T> 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) Commented Dec 5, 2012 at 16:25
  • @sehe - Memory is available allright but turns out there is a hardcoded 2GB limit to object size. Commented Dec 5, 2012 at 17:34

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

Almost but not quite; the issue boiled down to the 2GB limit for any object inside a managed .NET application.
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.

1 Comment

Eric Lippert put it this way 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.
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.

4 Comments

You could still overflow RAM on a 64bit machine with this code.
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.
@TheGreatCO A 32 bit system will error when doing this, a 64 bit system might error when doing this.
Yeah, creating an array that way is a waste of memory unless you are filling it all the way.
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.

Comments

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

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.