简体   繁体   English

带流的高 memory 使用解决方案

[英]High memory usage solutions with streams

I have a question about how high memory usage problems are solved in C#.我有一个关于在 C# 中解决多高 memory 使用问题的问题。

Let's say I am having a lot of item data in the database.假设我在数据库中有很多项目数据。 I want to read it all then write that data to separate files and archive it to a zip file.我想全部阅读,然后将该数据写入单独的文件并将其存档到 zip 文件中。 If I get all items data from database at one time, app would timeout cause all memory is used.如果我一次从数据库中获取所有项目数据,应用程序将超时,导致使用所有 memory。
What we did in our node js application we found a convenient library and used AsyncEnumerable and streamed items data in smaller pieces to a write stream.我们在 node js 应用程序中所做的工作是找到一个方便的库,并使用 AsyncEnumerable 和流式传输项目数据以较小的片段写入 stream。
So my question is how it is done in C#?所以我的问题是它是如何在 C# 中完成的? Because I haven't found an option how data could be streamed via Entity Framework?因为我还没有找到如何通过实体框架流式传输数据的选项? How it is solved and could anybody give some code snippets/examples of what could be used, is it IQueryable or something else?它是如何解决的,任何人都可以提供一些可以使用的代码片段/示例,是 IQueryable 还是其他? I'm interested in how data could be read from stream by 10000 elements at the time.我对当时如何从 stream 中读取 10000 个元素的数据感兴趣。

Data is streamed by default in EF, there's nothing for you to do.默认情况下,数据在 EF 中流式传输,您无需执行任何操作。 It's ToList() and friends that store the entire response in memory, if you just enumerate the queryable object it'll handle any streaming in the enumerator.将整个响应存储在 memory 中的是ToList()和朋友,如果您只是枚举可查询的 object,它将处理枚举器中的任何流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM