GC memory pressure problem in using unmanaged components

GC memory pressure problem in using unmanaged components

am 17.01.2008 15:18:02 von Noman Ali

Hi,

We are facing a strange problem in our ASP. NET website. Some times it gives
the following unhandled exception.

Error Message: Exception of type System.Web.HttpUnhandledException was thrown.
Detailed Error Message: System.InvalidOperationException: There has been an
overflow or underflow in GC memory pressure. The possible cause is unbalanced
AddMemoryPressure and RemoveMemoryPressure calls.
at System.MemoryWatcher.AddMemoryPressure(Int64 size)
at System.GC.AddMemoryPressure(Int64 pressure)
at WebApplication1.CFileUtils.ProcessFile(String filename)
at WebApplication1.CBanner.CreateStringTag(String appPath, Int32 width,
Int32 height, String& ext)
at WebApplication1.CAdRotator.DisplayBanners(HtmlTable table, String
position, String queryStr, String[]& displayed)
at WebApplication1.TopBanner.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()

I already did some research on it and found that it is caused when you add
and remove unbalanced memory pressure on GC. But we are not doing that. The
exception comes during peak hours of our site and it comes randomly normally
after 12-14 hours daily and after that we needs to restart the asp .net
worker process. The worker process uses around 50% of the system memory
during this abnormal behaviour. The web application heavily uses some
unmanaged COM components for PDF and image manupulation.

What i had done so far, i forefully calls garbage collection after every
hour so that COM components will be garbage collected. I am identifying
memory leaks in the web application but still i think the problem is
different.

Any suggestion, comments or experience will be welcome.

Regards,
Noman Ali

RE: GC memory pressure problem in using unmanaged components

am 17.01.2008 17:36:00 von brucebarker

you are probably "leaking" unmanaged com memory by not releasing com
references when done, but rather letting the gc do it.

when your code is done with a com object it should call
Marshal.ReleaseComObject. also you should never use more than one dot when
referencing a com object or you will have a leak.

ex:

// create com object
MyComObject a = new MyComObject();

// call 2 dot method wrong way
a.b.c(); // leaks b, only gc will release

// call 2 dot correct

MyComBType b = a.b;
b.c();
Marshal.ReleaseComObject(b);
b = null;

// release com object
Marshal.ReleaseComObject(a);
a = null;

tedious, but necessary.

-- bruce (sqlwork.com)


"Noman Ali" wrote:

> Hi,
>
> We are facing a strange problem in our ASP. NET website. Some times it gives
> the following unhandled exception.
>
> Error Message: Exception of type System.Web.HttpUnhandledException was thrown.
> Detailed Error Message: System.InvalidOperationException: There has been an
> overflow or underflow in GC memory pressure. The possible cause is unbalanced
> AddMemoryPressure and RemoveMemoryPressure calls.
> at System.MemoryWatcher.AddMemoryPressure(Int64 size)
> at System.GC.AddMemoryPressure(Int64 pressure)
> at WebApplication1.CFileUtils.ProcessFile(String filename)
> at WebApplication1.CBanner.CreateStringTag(String appPath, Int32 width,
> Int32 height, String& ext)
> at WebApplication1.CAdRotator.DisplayBanners(HtmlTable table, String
> position, String queryStr, String[]& displayed)
> at WebApplication1.TopBanner.Page_Load(Object sender, EventArgs e)
> at System.Web.UI.Control.OnLoad(EventArgs e)
> at System.Web.UI.Control.LoadRecursive()
> at System.Web.UI.Control.LoadRecursive()
> at System.Web.UI.Control.LoadRecursive()
> at System.Web.UI.Control.LoadRecursive()
> at System.Web.UI.Control.LoadRecursive()
> at System.Web.UI.Page.ProcessRequestMain()
>
> I already did some research on it and found that it is caused when you add
> and remove unbalanced memory pressure on GC. But we are not doing that. The
> exception comes during peak hours of our site and it comes randomly normally
> after 12-14 hours daily and after that we needs to restart the asp .net
> worker process. The worker process uses around 50% of the system memory
> during this abnormal behaviour. The web application heavily uses some
> unmanaged COM components for PDF and image manupulation.
>
> What i had done so far, i forefully calls garbage collection after every
> hour so that COM components will be garbage collected. I am identifying
> memory leaks in the web application but still i think the problem is
> different.
>
> Any suggestion, comments or experience will be welcome.
>
> Regards,
> Noman Ali

RE: GC memory pressure problem in using unmanaged components

am 21.01.2008 06:59:00 von Noman Ali

Hi bruce barker,

Thanks for the reply. There is a probablity of memory leak but i dont think
that the given exception is a result of memory leak. Normally memory leaks
results in an out of memory exception. However the given exception is related
to unbalaced addition in GC memory pressure.

The important thing is that we are using ASP . NET 1.1 and
System.GC.AddMemoryPressure is not available in .NET 1.1 version.

Regards,
Noman Ali

"bruce barker" wrote:

> you are probably "leaking" unmanaged com memory by not releasing com
> references when done, but rather letting the gc do it.
>
> when your code is done with a com object it should call
> Marshal.ReleaseComObject. also you should never use more than one dot when
> referencing a com object or you will have a leak.
>
> ex:
>
> // create com object
> MyComObject a = new MyComObject();
>
> // call 2 dot method wrong way
> a.b.c(); // leaks b, only gc will release
>
> // call 2 dot correct
>
> MyComBType b = a.b;
> b.c();
> Marshal.ReleaseComObject(b);
> b = null;
>
> // release com object
> Marshal.ReleaseComObject(a);
> a = null;
>
> tedious, but necessary.
>
> -- bruce (sqlwork.com)
>
>
> "Noman Ali" wrote:
>
> > Hi,
> >
> > We are facing a strange problem in our ASP. NET website. Some times it gives
> > the following unhandled exception.
> >
> > Error Message: Exception of type System.Web.HttpUnhandledException was thrown.
> > Detailed Error Message: System.InvalidOperationException: There has been an
> > overflow or underflow in GC memory pressure. The possible cause is unbalanced
> > AddMemoryPressure and RemoveMemoryPressure calls.
> > at System.MemoryWatcher.AddMemoryPressure(Int64 size)
> > at System.GC.AddMemoryPressure(Int64 pressure)
> > at WebApplication1.CFileUtils.ProcessFile(String filename)
> > at WebApplication1.CBanner.CreateStringTag(String appPath, Int32 width,
> > Int32 height, String& ext)
> > at WebApplication1.CAdRotator.DisplayBanners(HtmlTable table, String
> > position, String queryStr, String[]& displayed)
> > at WebApplication1.TopBanner.Page_Load(Object sender, EventArgs e)
> > at System.Web.UI.Control.OnLoad(EventArgs e)
> > at System.Web.UI.Control.LoadRecursive()
> > at System.Web.UI.Control.LoadRecursive()
> > at System.Web.UI.Control.LoadRecursive()
> > at System.Web.UI.Control.LoadRecursive()
> > at System.Web.UI.Control.LoadRecursive()
> > at System.Web.UI.Page.ProcessRequestMain()
> >
> > I already did some research on it and found that it is caused when you add
> > and remove unbalanced memory pressure on GC. But we are not doing that. The
> > exception comes during peak hours of our site and it comes randomly normally
> > after 12-14 hours daily and after that we needs to restart the asp .net
> > worker process. The worker process uses around 50% of the system memory
> > during this abnormal behaviour. The web application heavily uses some
> > unmanaged COM components for PDF and image manupulation.
> >
> > What i had done so far, i forefully calls garbage collection after every
> > hour so that COM components will be garbage collected. I am identifying
> > memory leaks in the web application but still i think the problem is
> > different.
> >
> > Any suggestion, comments or experience will be welcome.
> >
> > Regards,
> > Noman Ali