Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out Of Memory exception on System.Drawing.Image.FromFile()

I have an image uploader and cropper which creates thumbnails and I occasionally get an Out Of Memory exception on the following line:

Dim bm As Bitmap = System.Drawing.Image.FromFile(imageFile) 

The occurance of the error is tiny and very rare, but I always like to know what might be causing it. The imageFile variable is just a Server.MapPath to the path of the image.

I was curious if anyone had experience this issue previously and if they had any ideas what might be causing it? Is it the size of the image perhaps?

I can post the code if necessary and any supporting information I have, but would love to hear people's opinions on this one.

like image 531
dooburt Avatar asked Jul 10 '09 09:07

dooburt


People also ask

How do you handle system out of memory exception?

using System; public class Example { public static void Main() { try { // Outer block to handle any unexpected exceptions. try { string s = "This"; s = s. Insert(2, "is "); // Throw an OutOfMemoryException exception. throw new OutOfMemoryException(); } catch (ArgumentException) { Console.

What causes a memory exception?

Causes of such memory errors may be due to certain cognitive factors, such as spreading activation, or to physiological factors, including brain damage, age or emotional factors. Furthermore, memory errors have been reported in individuals with schizophrenia and depression.

Is insufficient memory an exception?

Conclusion. The OutOfMemoryException is a runtime exception that tells the programmer that there is no enough memory or there is a lack of contiguous memory for the allocations required by the C# program. To avoid this exception the user should always take necessary precautions and should handle this exception.


1 Answers

It's worth knowing that OutOfMemoryException doesn't always really mean it's out of memory - particularly not when dealing with files. I believe it can also happen if you run out of handles for some reason.

Are you disposing of all your bitmaps after you're done with them? Does this happen repeatably for a single image?

like image 84
Jon Skeet Avatar answered Oct 12 '22 02:10

Jon Skeet