0

I get these errors every once in a while and I am not sure why. This code executes thousands of times a day and Ill get these errors every once in a while. One of the images is 94.9 KB, 1024x1024 image. The image is being read from an Azure File Storage disk via UNC Path.

System.OutOfMemoryException: Out of memory.

Generated: Sat, 23 Apr 2016 15:09:54 GMT

System.OutOfMemoryException: Out of memory.
   at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
   at System.Drawing.Image.FromFile(String filename)
   at Tournaments.ImageHandler.ProcessRequest(HttpContext context) in C:\Development\Exposure\Main\Websites\Tournaments\ImageHandler.ashx.cs:line 64
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Actual Code

 using (var image = Image.FromFile(path))
 {
 }
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • It suggests there is a memory leak somewhere. – randominstanceOfLivingThing Apr 23 '16 at 15:16
  • I posted the code above. Memory leak within the .NET framework? – Mike Flynn Apr 23 '16 at 15:20
  • 1
    The memory leak is likely in `Tournaments.ImageHandler.ProcessRequest`, however, this can also be caused by a image that is just too large, or that has bad header data. – Scott Chamberlain Apr 23 '16 at 15:23
  • Well I opened the same URL in the browser and loaded fine. It is a big image though but why would this cause this? The image really isnt that large as I posted a size of one above. – Mike Flynn Apr 23 '16 at 15:25
  • It always looks like a library problem. You should take a heap dump and look at classes that are part of your application namespace. The problem usually occurs in application code. – randominstanceOfLivingThing Apr 23 '16 at 15:32
  • 2
    Note that GDI+ is really bad at identifying the reason for the OutOfMemoryException. It could be that the Image Header is currupted, or a number of different reasons! Check out the answer on [**this**](http://stackoverflow.com/questions/3848132/out-of-memory-image-fromfile) question! – MrPaulch Apr 23 '16 at 15:58
  • What is the handler doing with the image after it's loaded? Since you are loading it as an image rather than just as a file, you are probably manipulating it somehow. The manipulation code may be leaking memory if you are not consistently `using` every `IDisposable` object you create... – user1429080 Apr 23 '16 at 18:41

1 Answers1

2

This seemed to fix my issue as it doesn't hold a reference to it this way.

using (var memoryStream = new MemoryStream(File.ReadAllBytes(path)))
            {
                using (var image = Image.FromStream(memoryStream))
                {
                  
                    byte[] bytes;

                
                      
                        using (var memoryStream1 = new MemoryStream())
                        {
                            image.Save(memoryStream1, GetImageFormat(Path.GetExtension(path)));
                            bytes = memoryStream1.ToArray();
                        }
                    

                }
            }
    }
    
      private ImageFormat GetImageFormat(string extension)
            {
                switch (extension.ToLower())
                {
                    case ".png":
                        return ImageFormat.Png;
                    default:
                        return ImageFormat.Jpeg;
                }
            }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • This fix did not work for me. I have the same access denied or Out of Memory error as if I used `using (Image MyImage = System.Drawing.Image.FromFile(FileName))` or `using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))` – Adrian Hood Sr Jul 19 '21 at 02:01
  • Looks like I am using different code now. – Mike Flynn Jul 26 '21 at 13:22