GDI+ (GdiPlus) and ASP.NET

So we occasionally see customers that are either using System.Drawing themselves in their ASP.NET application or are using controls (like charting controls) that use it.

Under most circumstances, this works just fine.  But there are times where it does not, and this is cautioned to users on the System.Drawing page on MSDN.

We recently had a case where GDI+ was causing memory to jump up very quickly.  Looking at the heaps in DebugDiag showed us:


This shows the GdiPlus heap being very fragmented.  And looking at the crt heap, where GDI will allocate things:


If you look at the full picture, you will see that there is an allocation size of 2,691,448 which is taking up 782.86 MB.

There is a very good description of this on:
Charting in ASP.NET (alas: DGI+ not supported in a service)

Note that there are also sites which talk about using System.Drawing on ASP.NET, for example:

While this code will work, I would caution that if you run into problems, don't be surprised if this is what is causing the problem, especially if it gets heavy use on your site.

kick it on

Comments (11)

  1. You’ve been kicked (a good thing) – Trackback from

  2. Niki says:

    So, what was the problem? I mean, some code must have allocated all that memory and never properly freed it. Is there a memory leak in GDI+? In GDI? In System.Drawing? Will it be fixed?? Or did the customer forget to call Dispose on some GDI+ objects?

  3. Yes, so in this case, something did allocated the memory.  The problem is that sometimes you don’t know when to clean these objects up and they end up staying around for a long time.  In this case, something was holding onto the objects and keeping it from getting cleaned up.

    It also could easily have been that they didn’t clean up all the memory they allocated.

  4. Francois says:

    That has always bothered me a bit… sure, System.Drawing isn’t supported in certain senarios, and the documentation is incredibly clear on it…

    however, what exactly is the alternative? Making my own image manipulation stack?

  5. For the scenarios where this is affected, there isn’t really a good answer.  System.Drawing assumes that it has an interactive user so it will try to show dialog boxes and things under some circumstances.

    The best way is to test a lot and see what could go wrong and then handle those situation before it goes to System.Drawing so you don’t have the problem.   In this case, just making sure all objects get cleaned up will cut down the memory.

  6. Roger Martin says:

    This has been driving me nuts as well. I run an open source gallery web app and sometimes the memory will continue increasing until it crashes. I’ve studied it to death to make sure I’m not missing any dispose calls.

    Is it possible to improve things by forcing a garbage collection? I’m thinking the memory is locked so it won’t get freed even during a forced collection. Grrr.

  7. I wouldn’t suggest forcing a garbage collection.  that won’t really get you what you want.  So there are a few things that you can do.  Depending, you may want to create a case with Microsoft Support and have us help you track it down.

    To create a case, check out:

    There are a few things to look for.  The easiest thing would be to make sure you aren’t adding the objects to the cache or anything that would keep them rooted.  You can use some of the High Memory posts here to try to track down the problem:

  8. Josh Coswell says:

    I think I have seen this type of behaviour of GDI in the past.

    With this new insight I will have better chance to catch such problems.


  9. Ostwald says:

    Hey tom, I have a lil query in GDI . I want to extract the co-ordinates of the text from the image . I’ve already applied sobels algorithm and some morphing techniques. But i’m not gettint the exact coordinates so that i can draw a rectangle on that text. How can  i do this?

  10. I am not an expert in GDI.  I would suggest that you try asking this question on the forum or newsgroup for GDI.  Like:

    Or you can create a support case with Microsoft if you need the answer faster.  See my comment above for how to create one.

  11. Kelvin says:

    This problem is weird and sometime make us difficult to explain to customers.

    In the latest .Net framework, is there a perfect solution for manipulating images in web application?

    The "perfect" I mean is no memoery leak.

Skip to main content