2

I'm getting an OutOfMemory Exception when trying to resize larger images. Our server can only resize images of less then 1000x1000 pixels. My development machine seems too handle any size, and other developer machines seem to have the same limitations as the server. My development machine is also the best spec'd.

I feel like this code properly disposes of all the objects, but i could be wrong. I've tried using perfmon to view the .Net CLR memory but I'm unsure of how to interpret the results.

I'm stuck as to how to best solve this problem and get a definitive answer on why the problem exists. Any debugging ideas would be greatly appreciated.

Edit: Error occurs on g.DrawImage

using(Image imgToResize = Image.FromFile(path))
        {
            using (Bitmap b = new Bitmap(ResizeWidth, ResizeHeight, PixelFormat.Format24bppRgb))
            {
                using(Graphics g = Graphics.FromImage(b))
                {
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(imgToResize, 0, 0, ResizeWidth, ResizeHeight);
                    b.Save(DiskPathThumb(maxSize), ImageFormat.Jpeg);
                }
            }
        }
9
  • where do you invoke this code, in OnPaint event? Commented Mar 7, 2011 at 7:21
  • its invoked post file upload to the server. Sometimes its not even post upload its just when the thumbnail is requested. Either way gives the same result. Commented Mar 7, 2011 at 9:33
  • 1
    What line is it failing on? Is it always the same one? Also, what are the values of ResizeWidth and ResizeHeight? Sometimes GDI+ throws out of memory exceptions for things that aren't really out of memory exceptions. Commented Mar 8, 2011 at 8:08
  • sorry, error occurs at g.DrawImage, the resize value will usually be quite small. Maybe between 100-300px. Commented Mar 9, 2011 at 0:02
  • 1
    Some GDI+ functions throws an OutOfMemory exception if the parameters are invalid. Though I can't see how that can happen in your code. Since your parameters seem to be always valid. And the doc for your overload of DrawImages says nothing about that either. Commented Mar 9, 2011 at 0:07

2 Answers 2

1

Can you screenshot counters for PrivateBytes, LOH size, #Gen 2 collections?

What type of application is this? How deep is this into your application code? How many objects do you have on the LOH? Since the buffer that is being returned definitely qualifies as a Large Object. Have you looked at the call stack, the state of the heap, and fragmentation when this call fires?

FWIW, you might try the same code in a C or C++ version that uses the GDI. Every one of the graphics related functions you are using is a wrapper around the GDI and testing that in a test application on the failing machines would help narrow things down to the .Net Framework and not something else.

I was not able to get an OOM Exception, but the largest value I could pass was around 19866x19866 before the function would throw an InvalidParameterException. This is on a 64bit Win7 targeting .Net 4 w/VS2010.

Sign up to request clarification or add additional context in comments.

Comments

0

Turns out this was somehow related to us using the SessionPageStatePersister this was causing our gen2 stack to rise steadily over time and at some point before the app pool was recycled our systems wasn't able to handle much at all.

We have since reverted to the standard hidden field view state and this error has vanished.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.