gpt4 book ai didi

c# - IIS应用程序池过程使用大量内存

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:22 24 4
gpt4 key购买 nike

我的IIS应用程序池进程之一存在一个非常奇怪的问题。最近,我一直在收到System.OutOfMemoryException错误,而我一直在试图弄清楚到底是怎么回事。基本上,我有一个脚本,该脚本使用Web服务从DAM中获取文件。然后,它检查文件是否将其存储为字节数组,然后使用Response输出文件。唯一有问题的是PDF,当它们超过20MB时,现在看来有时会引起错误。如果我增加了应用程序池中的内存,它将暂时解决该问题。我观看了w3wp.exe进程,发现有时我运行此脚本时会将内存增加到400MB,我们拥有的最大文件是45MB,这将导致这种行为的发生。这个问题似乎每天晚上都会消失,早晨会工作一段时间,然后再次开始执行相同的操作。此应用程序是c#asp.net应用程序。它在共享点内运行。

观看了一段时间的服务后,我确实注意到,由于这些PDF在浏览器窗口中呈现,因此在文件完全下载之前,它不会从内存中释放。这是有道理的,但我可以看到这在某种程度上是我的问题。如果我有几个人正在加载文件,平均(无文件下载)内存使用量为385,000 kb,则可以轻松达到900,000-1,100,000 KB,这是应用程序池的限制。

我不是在寻找确切的答案,而是更像是一个前进的方向,因为我全都没有想法。

最佳答案

当您将文件数据作为字节数组放入内存时,会对Web服务器造成很大压力。

而不是将整个文件数据存储在字节数组中,您应该尝试将文件流分块写入响应流。

伪示例:

context.Response.Buffer = false;

byte[] buffer = new byte[4096];
int bytesRead = 0;

using(var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
while ((bytesRead = stream.Read(buffer, 0 , buffer.Length)) > 0)
{
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Flush();
}
}


这里的想法是,每次读取文件流时,您只将一部分文件数据带入内存中,然后将其写入响应中。请注意, response buffering已被禁用,并且您可以将文件流替换为另一个Stream数据源(我从SQL数据库读取二进制数据时使用了这种方法)。

编辑:(响应如何将数据从SQL传输到HTTP响应)

为了从SQL Server数据库表(例如varbinary(max)列)流式传输数据,请在SqlCommand上使用顺序访问:

#region WriteResponse(HttpContext context, Guid id)
/// <summary>
/// Writes the content for a media resource with the specified <paramref name="id"/>
/// to the response stream using the appropriate content type and length.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to write content to.</param>
/// <param name="id">The unique identifier assigned to the media resource.</param>
private static void WriteResponse(HttpContext context, Guid id)
{
using(var connection = ConnectionFactory.Create())
{
using (var command = new SqlCommand("[dbo].[GetResponse]", connection))
{
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@Id", SqlDbType.UniqueIdentifier);
command.Parameters.AddReturnValue();

command.Parameters["@Id"].Value = id;

command.Open();

using(var reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
if(reader.Read())
{
WriteResponse(context, reader);
}
}
}
}
}
#endregion

#region WriteResponse(HttpContext context, SqlDataReader reader)
/// <summary>
/// Writes the content for a media resource to the response stream using the supplied <paramref name="reader"/>.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to write content to.</param>
/// <param name="reader">The <see cref="SqlDataReader"/> to extract information from.</param>
private static void WriteResponse(HttpContext context, SqlDataReader reader)
{
if (context == null || reader == null)
{
return;
}

DateTime expiresOn = DateTime.UtcNow;
string contentType = String.Empty;
long contentLength = 0;
string fileName = String.Empty;
string fileExtension = String.Empty;

expiresOn = reader.GetDateTime(0);
fileName = reader.GetString(1);
fileExtension = reader.GetString(2);
contentType = reader.GetString(3);
contentLength = reader.GetInt64(4);

context.Response.AddHeader("Content-Disposition", String.Format(null, "attachment; filename={0}", fileName));

WriteResponse(context, reader, contentType, contentLength);

ApplyCachePolicy(context, expiresOn - DateTime.UtcNow);
}
#endregion

#region WriteResponse(HttpContext context, SqlDataReader reader, string contentType, long contentLength)
/// <summary>
/// Writes the content for a media resource to the response stream using the
/// specified reader, content type and content length.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to write content to.</param>
/// <param name="reader">The <see cref="SqlDataReader"/> to extract information from.</param>
/// <param name="contentType">The content type of the media.</param>
/// <param name="contentLength">The content length of the media.</param>
private static void WriteResponse(HttpContext context, SqlDataReader reader, string contentType, long contentLength)
{
if (context == null || reader == null)
{
return;
}

int ordinal = 5;
int bufferSize = 4096 * 1024; // 4MB
byte[] buffer = new byte[bufferSize];
long value;
long dataIndex;

context.Response.Buffer = false;
context.Response.ContentType = contentType;
context.Response.AppendHeader("content-length", contentLength.ToString());

using (var writer = new BinaryWriter(context.Response.OutputStream))
{
dataIndex = 0;
value = reader.GetBytes(ordinal, dataIndex, buffer, 0, bufferSize);

while(value == bufferSize)
{
writer.Write(buffer);
writer.Flush();

dataIndex += bufferSize;
value = reader.GetBytes(ordinal, dataIndex, buffer, 0, bufferSize);
}

writer.Write(buffer, 0, (int)value);
writer.Flush();
}
}
#endregion

关于c# - IIS应用程序池过程使用大量内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6874721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com