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:

GDI-2

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

GDI-1

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:
http://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.

kick it on DotNetKicks.com

Published 19 March 08 12:31 by Tom

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# DotNetKicks.com said on March 19, 2008 12:33 PM:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# Niki said on March 19, 2008 2:59 PM:

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?

# Tom said on March 19, 2008 3:27 PM:

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.

# Francois said on March 19, 2008 3:43 PM:

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?

# Tom said on March 19, 2008 3:50 PM:

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.

# Roger Martin said on March 19, 2008 5:20 PM:

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.

# Tom said on March 19, 2008 11:42 PM:

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:

http://blogs.msdn.com/tom/archive/2007/11/15/contacting-tom.aspx

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:

http://blogs.msdn.com/tom/archive/tags/High+Memory/default.aspx

# Josh Coswell said on March 20, 2008 3:07 AM:

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.net

# Ostwald said on March 20, 2008 10:12 AM:

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?

# Tom said on March 20, 2008 10:39 AM:

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.

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker