slash shogdhe
slash shogdhe

Reputation: 4187

OutOfMemoryException error

i m just writing the two lines in my windows application project

double[] results = new double[100000000];

double[] results1 = new double[100000000];

on second line(results1 ) i am getting the Exception 'System.OutOfMemoryException' was thrown.

well is my system Configuration is

RAM- 4 gb

OS - Windows 7

Processor - intel core to duo 2.93 Ghz

how can i resolve this error and why this error occurs

ok as most of the answer are realted to generic list

       List<int> results1 = new List<int>();
         List<int> results2 = new List<int>();



        for (int i = 0; i <= 100000000; i++)
        {
            results1.Add(i);
            results2.Add(i);
        }

i am getting the same error here also

Upvotes: 1

Views: 579

Answers (7)

S&#246;ren
S&#246;ren

Reputation: 2731

As far as I know, a list works as following: if the length of a list is growing over 2^n, another 2^n elements are allocated. So at the point the list has a length of 67108864 (=2^26), it will allocate another 2^26 elements. That is also exactly the point where the error occures if i am running your code. If you take long variables instead of int, you will get the error at a length of 33554432, which is exactly 2^25. Which makes sense as well.

So your first list has - in the moment it becomes longer than 2^26 - already allocated a memory space of 2^27 * 4 byte, because an int takes 4 bytes, so that is 2^29 bytes. In the moment you try to allocate more space for the second list, your memory exceeds 2^30 bytes which is 1GB.

That is why I think there is a generell RAM limit and it is not only about free contiguous address space, but also about 1 GB RAM limit for your process. I am not sure where exactly this limit may come from.

There is some post here about similar problems on a 32 bit machine - i don't know whether your win 7 is 32 or 64 bit. EDIT: I now ran your application in 64 bit mode and the problem is gone. So I can just refer to that post above again. And you should change the platform in the properties of your project to 64 bit.

Only one questions stays: why do you want to have such a big list? This is really memory consuming and there are many ways to avoid such an allocation.

Hope that helps.

Upvotes: 1

Tim Lloyd
Tim Lloyd

Reputation: 38434

When creating arrays, the memory allocator will attempt to reserve the total amount of memory required at the point you assign it. In this case it will 8 bytes for each element. The OOM in this case is pretty straightforward, there is not enough memory to allocate such large arrays. In a standad 32bit Windows configuration you are limited to a maximum of 2GB of memory address space per process for user code and data, although in practice it often much less than this due to memory fragmentation for example. In the case of fragmentation the OOM is better interpreted as "cannot find a large enough contiguous memory section" when dealing with large arrays.

Upvotes: 8

Steinthor.palsson
Steinthor.palsson

Reputation: 6496

The reason of the error is that the array is so large that it takes up all the free ram on your system when you run the code. What Darin said is right. It makes no sense to make an array that big. It will take forever to iterate through and will take too much ram on most systems.

Upvotes: 0

Serge Wautier
Serge Wautier

Reputation: 21878

One possible reason is fragmentation of Virtual address space: The memory manager could find a first 800MB contiguous address space chunk. But it couldn't find a second one.

Upvotes: 2

esaj
esaj

Reputation: 16025

I might be wrong, but usually (meaning, at least in most languages) when you allocate an array, it means you are asking for a continuous block of memory. A single array of doubles with 100,000,000 elements will need 100000000 * 8 bytes of memory (+ possible overhead), roughly 800 megabytes. Even if you have said amount of memory free (in total), it doesn't necessarily mean that the free memory is continuous, it might be fragmented into multiple free areas of different sizes, and the allocation will fail.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062492

That is an 800MB array, which you are trying to load into a win32 process space (2GB). It needs to be contiguous, too, which makes it even harder. Simply - it cannot find a big enough contiguous block in the LOH to fit all this in one big lump.

Three options:

  • don't allocate such a huge array - do you really need that?
  • switch to a jagged array; it is easier to allocate multiple mid-size arrays; for example a double[][] with 10000 inner arrays each of length 10000 - just do some / and % code to get into the right block
  • switch to x64 (note the array is still hard-limited to 2GB)

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You shouldn't be allocating so large arrays that cannot fit in memory. Simply use a list and add elements as needed:

var results = new List<double>();

Quote from the documentation:

int[] array = new int[5];

This array contains the elements from array[0] to array[4]. The new operator is used to create the array and initialize the array elements to their default values. In this example, all the array elements are initialized to zero

So you need to have enough memory to hold those default values.

Upvotes: 5

Related Questions