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:
https://www.geekpedia.com/tutorial123_Working-with-GDIplus-in-ASP.NET.html
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.
Comments
Anonymous
March 19, 2008
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
March 19, 2008
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?Anonymous
March 19, 2008
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.Anonymous
March 19, 2008
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?Anonymous
March 19, 2008
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.Anonymous
March 19, 2008
The comment has been removedAnonymous
March 19, 2008
The comment has been removedAnonymous
March 19, 2008
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. Josh http://riverasp.netAnonymous
March 20, 2008
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?Anonymous
March 20, 2008
I am not an expert in GDI. I would suggest that you try asking this question on the forum or newsgroup for GDI. Like: http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.platformsdk.gdi&mid=27d0cbcb-9d41-4fbc-91ad-dec2fb7e7179 Or you can create a support case with Microsoft if you need the answer faster. See my comment above for how to create one.Anonymous
June 21, 2011
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 asp.net web application? The "perfect" I mean is no memoery leak.