OutOfMemory exception

c#, garbage-collection, out-of-memory

Solution

The OutOfMemory exception happens whenever a call to any of the following MSIL instructions fails

- newobj

- newarr

- box

Which basically the operations that allocate new memory in the heap, in your case the Stream.ReadToEnd apparently allocates array of bytes internally to load the stream in memory, so if the file in big enough to break the process it will throw this exception.

Problem

What are the possible reasons of `OutofMemory` exception. Memory allocations should be handled by GC. How much memory is allocated/Available to normal .NET/C# application In our application it comes at different places like `Stream.ReadToEnd()` and `DataTable.WriteXml(Memory stream)` function. Environment is .Net C#

Original source