【发布时间】:2015-08-01 05:40:44
【问题描述】:
有时,我们的 Web 应用程序会导致此错误:
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
该应用程序是 C# ASP.NET 4.5 Web Forms 应用程序,运行在具有 6G 内存的 Windows 2008 R2 服务器上。我们已经增加了几次内存,但它仍然会发生。所以我想知道它的应用程序是否有问题?可能是什么原因?
编辑
我有这个堆栈,不知道它是否有帮助:
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)
编辑
这是生成 PDF 的代码,它基于 XSL-FO 字符串。这会导致问题吗?
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();
}
编辑
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;
}
【问题讨论】:
-
内存不足的原因有很多,通常不是因为总内存。您能否发布导致问题的代码?没有它,我们真的无法给出一个好的答案。
-
这可能是由于代码问题,但也可能与服务器上发生的其他事情有关;服务器与许多组件共享内存,而其他东西可能会很麻烦。
-
听起来很正常的内存泄漏(对象不断在内存中累积)。在测试服务器上做一些内存分析。搜索诸如 .net 内存分析内存泄漏之类的术语如何
-
我们可以获得完整的堆栈跟踪吗?也许是一些代码处理文件上传,有人上传了一个 +inf 字节的大文件?您是否检查过性能监视器以查看不同 gc 代的大小?
标签: c# asp.net out-of-memory