Troubleshooting Exceptions: System.OutOfMemoryException
An OutOfMemoryException exception is thrown when an attempt to allocate memory fails.
Associated Tips
If you are creating an array, make sure the size is correct.
For more information, Visual Basic users can see Arrays in Visual Basic.For more information, C# users can see Arrays (C# Programming Guide).
Be sure you have enough memory for internal purposes and new managed objects.
If you are programming on the .NET Compact Framework, the common language runtime throws this exception when there is not enough memory for internal purposes or new managed objects. To prevent the exception, avoid programming large methods that consume 64 or more kilobytes of memory.
Remarks
Excessive managed memory usage is commonly caused by:
Reading large data sets into memory.
Creating excessive cache entries.
Uploading or downloading large files.
Excessive use of regular expressions or strings while parsing files.
Excessive view state.
Too much data in session state or too many sessions.
This exception may be thrown with an additional message, "Not enough storage is available to complete this operation," when invoking a method on a COM object that returns a user-defined type that contains a safe array (an array of non-fixed size). This is because the .NET Framework cannot marshal a structure field with a safe array type.