I have written the following code to check for sufficient memory,
while (true)
{
try
{
// Check for available memory.
memFailPoint = new
Consider using the GC.GetTotalMemory method to determine the amount of memory available before and after calling:
memFailPoint = new MemoryFailPoint(250);
InsufficientMemoryException is thrown before starting an operation, by the MemoryFailPoint constructor when you specify a projected memory allocation larger than the amount of currently available memory.
Like user7116 commented, that's why you should check first.
The example in this link should give you a solution: MemoryFailPoint Class
You can also check this msdn blog article: Out of memory? Easy ways to increase the memory available to your program