Reputation: 195
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?
Upvotes: 1
Views: 109
Reputation: 283763
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.
Upvotes: 6