title: From time to time we have System.OutOfMemoryException error on our asp.net web application
body: <p>From time to time, our web application will cause this error:</p>
  
  <pre><code>System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
  </code></pre>
  
  <p>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?</p>
  
  <p><strong><em>edit</em></strong></p>
  
  <p>I have this stack, don't know if it helps at all:</p>
  
  <pre><code>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&amp; completedSynchronously)
  </code></pre>
  
  <p><strong><em>edit</em></strong></p>
  
  <p>So this is the code that generates PDF, its based on XSL-FO string. Does this caused problem?</p>
  
  <pre><code>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();                    
   }
  </code></pre>
  
  <p><strong><em>edit</em></strong></p>
  
  <p>code for <code>ProcessFO()</code>:</p>
  
  <pre><code>    [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;
      }
  </code></pre>