4

I am trying to make an array which holds all the positive integers possible, I tried the following code and it allways throws out of memory exception.

private int[] AllIntegers()
{
    int[] all = new int[int.MaxValue];

    for (int i = 0; i < int.MaxValue; i++)
    {
        all[i] = i;
    }

    return all;
}

What I am doing wrong? or this is not possible at all?!

3
  • 1
    Why do you need such a large array? Commented Dec 18, 2011 at 11:21
  • Remember that such an array will take at least 8GB ram. Are you running a 32bit or 64 bit system ? Commented Dec 18, 2011 at 11:22
  • I am using win 7 64 bit and I got 6GB memory. The value is just expriment! Commented Dec 18, 2011 at 11:24

5 Answers 5

9

There's a hard upper limit on .NET object sizes, they cannot be larger than 2 gigabytes. Even on a 64-bit operating system. Your array is well beyond that size.

On a 32-bit operating system you'll never get close to that limit, the largest chunk of contiguous virtual memory available is around 650 megabytes, give or take. Only at startup, this goes down hill rapidly. This is a side effect of address space fragmentation, caused by having a mix of code and heaps in the address space. The total amount of memory you can allocate is close to 2 gigabytes, as long as the size of each allocation is small enough. Not something you'd ever want to get close to, random program failure due to OOM is hard to deal with.

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

Comments

4

Int.MaxValue = 2,147,483,647, sizeof(int) = 4, so you would need 8 GB of memory to allocate this array. The exception indicates that your OS is not able to allocate this amount of memory.

== UPDATE ==

MSDN:

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: BigArray<T>, getting around the 2GB array size limit

2 Comments

No. The error means that the OS can't allocate that much memory to the process. The user may have 8GB installed, but they might not be all addressable.
Even if he had that much memory, no .NET object can be larger than 2GB, I believe.
0

You are trying to allocate about 4 * (2^32 -1) bytes of memory. Thats exactly 8GB and likely more than your system can offer or rather what your process can allocate.

Comments

0

If you are on a 32 bit OS (on a 64 bit OS this MAY change), an integer is 4 bytes (32 bit). Thus the int.MaxValue is 2^31 (2^31 signed, 2^31 unsigned), so you are trying to allocate an array of 2^31 integers. Multiplying this for 4 bytes each, you obtain 8589934592 bytes, that is exactly 8 GBytes.

Comments

0

As others said, you can't keep all that info in memory at the same time. To get a list of all natural numbers, just use an iterator block, which only holds an int in memory at a time, plus the method status info:

static void Main (string[] args)
{
    foreach (int i in Program.NaturalNumbers ())
    {
        Console.WriteLine (i);
    }
}

public static IEnumerable<int> NaturalNumbers ()
{
    for (int i = 0; i <= int.MaxValue; i++)
    {
        yield return i;
    }
}

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.