Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemory exception

  1. What are the possible reasons of OutofMemory exception.

  2. Memory allocations should be handled by GC.

  3. 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#

like image 399
Buzz Avatar asked Oct 26 '09 09:10

Buzz


People also ask

How can Outofmemory exception be resolved?

1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

What is Outofmemory exception in C#?

An OutOfMemoryException exception has two major causes: You are attempting to expand a StringBuilder object beyond the length defined by its StringBuilder. MaxCapacity property. The common language runtime cannot allocate enough contiguous memory to successfully perform an operation.

What is Outofmemory error in Java?

OutOfMemoryError exception. Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap. In this case, The garbage collector cannot make space available to accommodate a new object, and the heap cannot be expanded further.


2 Answers

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

  1. newobj
  2. newarr
  3. 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.

like image 194
bashmohandes Avatar answered Oct 22 '22 09:10

bashmohandes


Either you are using more memory than the app has available to it. In this case you will need to work out how to make your memory usage more efficient. Using Files / Database to store data you arent immediately using may be necessary..

Or, you have a memory leak. In which case you need to look at removing references to memory when you are no longer using them so the GC can free up the memory.

If you are using C# or .Net you can use the CLR Profiler to analyse your memory to see how it is being used. CLR Profiler

like image 42
Mongus Pong Avatar answered Oct 22 '22 08:10

Mongus Pong