每次我想保存文件时如何避免创建FileStream?
我有一个应用程序,我每次都会收到一个流来保存它,所以我想创建一个全局 **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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果流需要保存到不同的文件中,我认为没有办法摆脱每次创建新流的麻烦。
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.
记住您所写的内容:“传入的流将保存在不同的文件中”,我强烈建议不要这样做。只需为每个文件创建一个新的 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.
您尝试过 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.