1

I'd like to create two 200M int elements arrays. Like that:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Arr
{
    class Program
    {

        static void Main(string[] args)
        {

            int Min = 0;
            int Max = 10;
            int ArrSize = 200000000;

            int[] test2 = new int[ArrSize];
            int[] test3 = new int[ArrSize];

            Console.ReadKey();

        }
    }
}

However, under VS2013, I'm getting out of memory exception with yellow arrow pointing on int[] test3 = new int[ArrSize]; line. The machine has 12GB of RAM.

If I decrease number of elements to 150M no exception is thrown.

If I initialise only one array of size 200M, no exception is thrown.

Why?

user1146081
  • 195
  • 15
  • No, this question has no answer in the link you sent. 200M ints is circa 800MB. Why this size is ok for first object but not for the second one? – user1146081 Mar 13 '15 at 22:53
  • This question definitely isn't about the size of a single object, but the total size of multiple objects. Reopened. – Ben Voigt Mar 13 '15 at 23:01
  • @PetSerAl - obviously 32bit. x64 will fail with single allocation at about 500M, 32 bit fails on attempt to find 2 continuous blocks of 800Mb (which is ridiculously hard), one usually ok. – Alexei Levenkov Mar 13 '15 at 23:07

1 Answers1

6

The machine has 12GB of RAM.

affects the speed of your program (depending on how much of that is free) but physical memory size has no effect on allocation success or failure. That's determined by available address space.

If you are compiling as 32-bit, especially if you are not using the /LARGEADDRESSAWARE option, then you will only be able to allocate a handful of large objects, because they have to fit into a single 2GB address space, and that space is broken up by DLLs and other allocations.

It's a good idea to use 64-bit if you really need objects that large. For 32-bit programs you can work around this partially by breaking your objects into smaller chunks, increasing the chance of finding a free area of address space big enough.

The default settings for a C# console app in VS 2013 are "AnyCPU", with "Prefer 32-bit".

You can change this setting on the Project Properties -> Build tab. enter image description here

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • It's important to note that the system space, needs to be contiguous, so even if your windows Resource Monitor says you have 2 GB of Free Memory in RAM, it might not be continuous memory. – Xantix Mar 13 '15 at 23:09
  • 2
    @Xantix: When I say "space is broken up", I'm talking about contiguity. And you completely missed the point if you are looking at Free RAM, because allocation failures are caused by lack of (contiguous) address space, not lack of RAM. If RAM is lacking, the swapfile gets used. – Ben Voigt Mar 13 '15 at 23:10