1

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

slash shogdhe
  • 3,943
  • 6
  • 27
  • 46

7 Answers7

8

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.

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
5

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • well i not about the replacement code, its about the reason of error – slash shogdhe Jan 29 '11 at 12:43
  • 1
    @slash shogdhe, the reason for the error is because when you use the `new` operator, memory is allocated and it assigns all the elements to their default values and assigning 100000000 x 8 bytes = 762MB doubles cannot hold into the memory you have available at the moment you are calling this. – Darin Dimitrov Jan 29 '11 at 12:44
  • A List<> doesn't solve the problem, it is still an array under the hood. – Hans Passant Jan 29 '11 at 14:53
  • @Hans, adding 100000000 into an array in memory doesn't seem like anything practical to do and the OP haven't really shown what *problem* he is trying to solve. – Darin Dimitrov Jan 29 '11 at 16:30
5

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)
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 2
    @slash - double=8 bytes, multiplied by 100000000 cells; divide by 1024 for KB, and again by 1024 for MB – Marc Gravell Jan 29 '11 at 12:49
  • How does the Jagged arrays solve the issue? I think you still require same amount of memory. Do I missing something? – Sandeep Feb 04 '11 at 04:31
  • @Sandeep because it doesn't have to be *contiguous*. In a fragmented large-object-heap you might be able to find enough *mid-size* blocks, but finding one great behemoth is a pain. Also, if desired it allows you to defer allocation of the upper blocks until you are sure you need them. – Marc Gravell Feb 04 '11 at 06:17
2

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.

esaj
  • 15,875
  • 5
  • 38
  • 52
2

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.

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
1

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.

Community
  • 1
  • 1
Sören
  • 2,661
  • 2
  • 19
  • 22
0

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.

Steinthor.palsson
  • 6,286
  • 13
  • 44
  • 51
  • 3
    OOM isn't about free RAM **at all**, since computers use virtual memory and dynamic mapping extensively. It is about mapping that to the virtual address space. http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx – Marc Gravell Jan 29 '11 at 12:51