OutOfMemoryException异常产生巨大的字符串中的ASP.NET时 [英] OutOfMemoryException when creating huge string in ASP.NET

查看:151
本文介绍了OutOfMemoryException异常产生巨大的字符串中的ASP.NET时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在导出大量数据的字符串(CSV格式),我得到一个OutOfMemoryException异常。什么是解决这一问题的最佳方法是什么?该字符串返回到Flex应用程序。

我会做的是CSV导出到服务器磁盘,并给回一个网址的Flex。这样,我可以刷新流写入到磁盘。

更新:

字符串是建立一个StringBuilder的:

  StringBuilder的StringBuilder的=新的StringBuilder();
字符串分隔符=;
布尔showUserData = TRUE;

//从sessionwarehouse数据
名单< D​​whSessionDto>采集
     = _dwhSessionRepository.GetByTreeStructureId(treeStructureId);

//将头
stringbuilder.Append(用户ID+分隔符);
如果(showUserData)
{
    stringbuilder.Append(名字+分隔符);
    stringbuilder.Append(名字+分隔符);
}
stringbuilder.Append(SessionID的+分隔符);
stringbuilder.Append(TreeStructureId+分隔符);
stringbuilder.Append(姓名+分隔符);
stringbuilder.Append(分数+分隔符);
stringbuilder.Append(MaximumScore+分隔符);
stringbuilder.Append(MinimumScore+分隔符);
stringbuilder.Append(ReducedScore+分隔符);
stringbuilder.Append(ReducedMaximumScore+分隔符);
stringbuilder.Append(持续时间+分隔符);
stringbuilder.AppendLine(类别+分隔符);

的foreach(在集合VAR dwhSessionDto)
{
    stringbuilder.Append(
        getPackageItemsInCsvFromDwhSessionDto(
            dwhSessionDto,分隔符,showUserData));
}

返回stringbuilder.ToString();
 

该字符串被送回到Flex是这样的:

  VAR的contentType =文/ CSV;
字符串结果= exportSessionService.ExportPackage(treeStructureId);
//写出口到响应
_context.Response.ContentType的contentType =;
_context.Response.AddHeader(内容处置,
    的String.Format(附件;文件名= {0},treeStructureId +名为.csv));

//不要遗漏因人而异星这样的高速缓存在客户端将被禁用
_context.Response.Cache.SetOmitVaryStar(假);
_context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

如果(!String.IsNullOrEmpty(结果))
{
    _context.Response.Output.Write(结果);
    _context.Response.Output.Close();
}
其他
{
    _context.Response.Output.Write(无日志);
}
 

解决方案
  

字符串与一个StringBuilder创建并传回Flex应用程序,HttpContext.Reponse.Output.Write(结果); - 列文Cardoen

那么,你基本上做的是这样的:

  StringBuilder的海峡=新的StringBuilder();
同时,(无论)
{
    str.Append(thisNextLineIGuess);
}
Response.Output.Write(str.ToString());
 

正确的?这可能不是你的确切code,但它听起来像是一个好的近似。

那么解决方法很简单:

 ,而(其他)
{
    Response.Output.Write(thisNextLineIGuess);
}
 

流通常比缓冲整个事情并发送它作为一个肿块更好。

When exporting a lot of data to a string (csv format), I get a OutOfMemoryException. What's the best way to tackle this? The string is returned to a Flex Application.

What I'd do is export the csv to the server disk and give back an url to Flex. Like this, I can flush the stream writing to the disk.

Update:

String is build with a StringBuilder:

StringBuilder stringbuilder = new StringBuilder();
string delimiter = ";";
bool showUserData = true;

// Get the data from the sessionwarehouse
List<DwhSessionDto> collection 
     =_dwhSessionRepository.GetByTreeStructureId(treeStructureId);

// ADD THE HEADERS
stringbuilder.Append("UserId" + delimiter);
if (showUserData)
{
    stringbuilder.Append("FirstName" + delimiter);
    stringbuilder.Append("LastName" + delimiter);
}
stringbuilder.Append("SessionId" + delimiter);
stringbuilder.Append("TreeStructureId" + delimiter);
stringbuilder.Append("Name" + delimiter);
stringbuilder.Append("Score" + delimiter);
stringbuilder.Append("MaximumScore" + delimiter);
stringbuilder.Append("MinimumScore" + delimiter);
stringbuilder.Append("ReducedScore" + delimiter);
stringbuilder.Append("ReducedMaximumScore" + delimiter);
stringbuilder.Append("Duration" + delimiter);
stringbuilder.AppendLine("Category" + delimiter);

foreach (var dwhSessionDto in collection)
{
    stringbuilder.Append(
        getPackageItemsInCsvFromDwhSessionDto(
            dwhSessionDto, delimiter, showUserData));
}

return stringbuilder.ToString();

The string is sent back to Flex like this:

var contentType = "text/csv";
string result = exportSessionService.ExportPackage(treeStructureId);
// Write the export to the response
_context.Response.ContentType = contentType;
_context.Response.AddHeader("content-disposition", 
    String.Format("attachment; filename={0}", treeStructureId + ".csv"));

// do not Omit the Vary star so the caching at the client side will be disabled
_context.Response.Cache.SetOmitVaryStar(false);
_context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

if (!String.IsNullOrEmpty(result))
{
    _context.Response.Output.Write(result);
    _context.Response.Output.Close();
}
else
{
    _context.Response.Output.Write("No logs");
}

解决方案

String is created with a StringBuilder and communicated back to Flex Application with HttpContext.Reponse.Output.Write(result); – Lieven Cardoen

So what you're basically doing is this:

StringBuilder str = new StringBuilder();
while(whatever)
{
    str.Append(thisNextLineIGuess);
}
Response.Output.Write(str.ToString());

Correct? That may not be your exact code, but it sounds like a good approximation.

Then the solution is simple:

while(whatever)
{
    Response.Output.Write(thisNextLineIGuess);
}

Streaming is usually better than buffering the whole thing and sending it out as a lump.

这篇关于OutOfMemoryException异常产生巨大的字符串中的ASP.NET时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆