What is OutOfMemory exception? How to identify them? How to avoid?

What is OutOfMemory?

OutOfMemoryException will be thrown when there is not enough chuck of memory for the program to allocate. The problem can be tricky sometimes as the stack traces are not always pointing to the right places (where memory leaks occur), due to the location of the exception thrown could be anywhere the program tries to allocate memory while there is not enough.

The causes of this type of exception can be summarised as below:

To identify the causes of OutOfMemory exception, here are two things we can do:

Symptoms and Analysis:

OOM1

OOM2

OOM3

OOM4

Reference: OutOfMemoryException in C#.