Hiraeth
Hiraeth

Reputation: 203

From time to time we have System.OutOfMemoryException error on our asp.net web application

From time to time, our web application will cause this error:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.

The application is C# ASP.NET 4.5 Web Forms application, running on Windows 2008 R2 server with 6G memory. We have increased memory several times, but it still happens. So I am wondering if its application's problem? What could be the reason?

edit

I have this stack, don't know if it helps at all:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.

Generated: Wed, 29 Jul 2015 08:49:24 GMT

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at ASP.popups_downloadtrendpdf_aspx.__BuildControlTree(popups_downloadtrendpdf_aspx __ctrl)
   at ASP.popups_downloadtrendpdf_aspx.FrameworkInitialize()
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.popups_downloadtrendpdf_aspx.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

edit

So this is the code that generates PDF, its based on XSL-FO string. Does this caused problem?

PDFGenerator.PDFGenerator svc = new PDFGenerator.PDFGenerator();
byte[] myfile = svc.ProcessFO(foString);
if (myfile != null)
{
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.Clear();
    response.AddHeader("Content-Type", "application/pdf");
    response.AddHeader("Content-Disposition", "attachment; filename=" + filename + "; size=" + myfile.Length.ToString());
    response.Flush();
    response.BinaryWrite(myfile);
    response.Flush();                    
 }

edit

code for ProcessFO():

    [WebMethod]
    public byte[] ProcessFO(string requestFO)
    {
        byte[] result = null;
        FODocument doc = new FODocument();
        try
        {
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(requestFO);
            using (MemoryStream stream = new MemoryStream(byteArray))
            {
                using (MemoryStream pdfStream = new MemoryStream())
                {
                    doc.generate(stream, pdfStream);
                    result = pdfStream.ToArray();
                }
            }
        }
        catch (System.Exception exc)
        {
            logger.Debug("Trouble in ProcessFO: " + exc.Message);
            logger.Debug(exc.StackTrace);
        }
        return result;
    }

Upvotes: 0

Views: 1464

Answers (2)

Dmitry S.
Dmitry S.

Reputation: 8513

Looking at the stack trace I see "downloadtrendpdf". Is the app buffering PDF files in memory for upload/download operations? A lot WebForms controls are notorious for doing that. That will make you run out of memory quickly.

Upvotes: 0

Samuel Beland-Leblanc
Samuel Beland-Leblanc

Reputation: 51

This looks like you have a memory leak. Now I won't get into the virtual memory vs physical memory, but the solution here is not to increase the memory. You have to fix the part of your code that "leaks".

In .net, there could be a lot of possibilities (event handlers not released (well maybe more in desktop apps), disposal of unmanaged memory, etc.), so without the code, this answer can't get much into details.

You should try some memory profilers (start a trial of dotMemory or redgate's memory profiler). Or, look for WinDbg with the sos library.

Upvotes: 2

Related Questions