如何降低 FileStreamResult api 中的内存使用量 [英] How to lower memory usage in FileStreamResult api
问题描述
现在我有一个可以得到很多结果 (800k) 的 api,它应该解析它们并将它们作为 CSV 传输.我拥有的(那种)工作代码是这个
Right now I have an api that gets many results (800k) and it should parse and stream them as CSV. The (kind of) working code I have is this
var query = context.address.AsNoTracking();
MemoryStream memoryStream = new MemoryStream();
StreamWriter writer = new StreamWriter(memoryStream);
foreach (var row in query)
{
writer.WriteLine(row.Name+","+row.Surname+","+row.BirthDate);
}
writer.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/octet-stream")
{
FileDownloadName = "DataFile.csv"
};
然而,这似乎将所有内容都放入了内存中,并使用了 300 mb 的内存来存储它.据我了解,这发生在 foreach 语句中.有没有办法让我写行,流式传输,然后从内存中处理它?我的目标是在不使用太多内存的情况下实现类似的东西
However this seems to put everything into memory, and store it using up 300 mb of memory. As i understand it, that happens in the foreach statement. Is there a way that i write the line, stream it, and then dispose of it from memory? My goal is to achieve something similar to this without using so much memory
我也试过这种方法,一次调用有效,但是如果同时进行多个 api 调用,就会出现奇怪的行为
I have also tried it this way, which works for one call, but if multiple api calls are made at the same time, there is weird behaviour
public async Task makeDif()
{
//var query = context.Adresar.AsNoTracking();
var query = context.Adresar.Select(x => string.Join(",", x.Ime, x.Prezime, x.Datarag + Environment.NewLine)).AsNoTracking();
Response.ContentType = "application/json";
Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes("ime,prezime,datarag" + Environment.NewLine));
foreach (var row in query)
{
await Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(row));
}}
推荐答案
您可以将数据直接写入响应体,无需将其放入 MemoryStream.
You can write data directly to response body without need of putting it into a MemoryStream.
提问者这就是我所做的
//var query = context.Adresar.AsNoTracking();
var query = context.Adresar.Select(x => string.Join(",", x.Ime, x.Prezime, x.Datarag + Environment.NewLine)).AsNoTracking();
Response.ContentType = "application/json";
Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes("ime,prezime,datarag" + Environment.NewLine));
foreach (var row in query)
{
await Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(row));
}
它似乎工作正常
这篇关于如何降低 FileStreamResult api 中的内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!