每次我想保存文件时如何避免创建FileStream?

发布于 2024-11-24 22:28:43 字数 250 浏览 4 评论 0原文

我有一个应用程序,我每次都会收到一个流来保存它,所以我想创建一个全局 **FileStream** 而不是每次获取文件时都创建一个。有办法做到这一点吗? (有没有FileStream的替代品)可以做到这一点? 因为我的应用程序中已经有一个大对象(LO),它影响了 CLR 性能。 PS:传入的流将保存在不同的文件中(不同的文件名) 更新: 我已经使用短语创建它,但 GC.Collect 受到我的应用程序中创建的 LO 的影响

I have an application which i receive a stream every while to save it, so i want to create a global **FileStream** instead of creating every time i get a file. Is there anyway to do that? (Is there any alternative to FileStream) can do this?
because i already have a large object(LO) in my application which affected the the CLR performance.
P.S: the incoming streams would be saved in different files(different file name)
UPDATE:
I alreay create it in using phrase but the GC.Collect is affected by the LO created in my application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揪着可爱 2024-12-01 22:28:43

如果流需要保存到不同的文件中,我认为没有办法摆脱每次创建新流的麻烦。

If the streams need to be saved to different files i don't think there is a way of getting away from creating a new stream each time.

悲喜皆因你 2024-12-01 22:28:43

记住您所写的内容:“传入的流将保存在不同的文件中”,我强烈建议不要这样做。只需为每个文件创建一个新的 FileStream,并在完成后尽快关闭。

问候。

Having in mind what you wrote: " the incoming streams would be saved in different files" I would strongly suggest to not do that. Just create a new FileStream for every file and close as soon as you finished, as fast as you can.

Regards.

满意归宿 2024-12-01 22:28:43

您尝试过 System.IO.File.WriteAllBytes 吗?

我认为 FileStream 对象在内存中不会很大。当它刷新时,内容被写入文件。

为了避免触及大对象堆,您可以使用 List 并将字节分块为预定义的大小。

Have you tried System.IO.File.WriteAllBytes?

The FileStream object won't be very large in memory I don't think. When it flushes, the content is written to file.

To avoid touching the large object heap, you could use a List<byte[]> and chunk the bytes into predefined sizes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文