0

I am getting Out of Memory exception in my C# application while processing images. I am testing it on my computer where I have 8GB of ram, way more than used by the app.

I've checked the GC.TotalMemory(false). Before the processing runs it says (converted to MB) 2MB, after without waiting for the garbage collection it says 4MB. While processing it reaches 81 MB at the peak.

I first thought that it something to do with the binding restrictions, because it works inside a WCF service, but didn't find any parameter that can lead to that exception.

I think that my application should have no problem running with 81MP memory used at the peak, and even more. What I am doing wrong? -- Thanks.

The loop that runs the processing:

Parallel.For(0, count, i =>
   {
      Task<int>.Factory.FromAsync(proxy.BeginSaveImage(sp, new AsyncCallback(CompleteSave), state), proxy.EndSaveImage).ContinueWith(result => {});
});

Runs in parallel and calls the image processing method asynchronously.

Liron Harel
  • 10,819
  • 26
  • 118
  • 217

1 Answers1

2

For Legacy reasons, Image.FromFile will throw out of memory exceptions for image files it cannot read. See Is there a reason Image.FromFile throws an OutOfMemoryException for an invalid image format?

Your png may be invalid and throwing out of memory exceptions for similar reasons

Community
  • 1
  • 1
berkeleybross
  • 1,314
  • 2
  • 13
  • 27