C# - Out of Memory Exception

C# - Out of Memory Exception

c#, systemoutofmemory

While programming for my web app the other day, my application threw an System.OutOfMemoryException when initializing a list of objects. To me this seemed pretty much impossible because my machine has 4GB RAM. Specifically the error happened when I tried to add an existing collection to a new list. For example:

List newShapes = new List(selectedShapes);

My original thought was that this assignment shouldn't allocate much memory since the shapes from my list should already exist inside memory. The Shape class is a complex object and I was trying to assign about 25,000 items to the new list at once. We should also note that the total size of all Shapes in the application come from a database that is only about 250MB in size. At first I was stumped and had no idea what may be causing the System.OutOfMemoryException. 

What happened:

  1. If you are running a 32 bit Windows, you won't have all the 4GB accessible, only 2GB per object array. This is a limitation of the .NET framework. (This happened to be my issue, as my shape array was greater than 2GB after debugging..)
  2. Don't forget that the underlying implementation of List is an array. If your memory is heavily fragmented, there may not be enough contiguous space to allocate your List, even though in total you have plenty of free memory.

 Solutions:

  1. If you are running on 64 bit Windows and you have enough free memory, ensure that the platform target is set correctly in the project properties > build properties.
    System Out Of Memory Fix
  2. If you are using .NET 4.5 or greater you can add this line to the runtime section of your App.Config:

<runtime> 
<gcallowverylargeobjects enabled="true"></gcallowverylargeobjects>
</runtime>