开发者

File open memory c#

开发者 https://www.devze.com 2022-12-16 09:46 出处:网络
While opening a file in C# using stream reader is the file going to remain in memory till it closed.

While opening a file in C# using stream reader is the file going to remain in memory till it closed. For eg if a file 开发者_如何学运维of size 6MB is opened by a program using streamreader to append a single line at the end of the file. Will the program hold the entire 6 MB in it's memory till file is closed. OR is a file pointer returned internally by .Net code and the line is appended at the end. So the 6MB memory will not be taken up by the program


The whole point of a stream is so that you don't have to hold an entire object in memory. You read from it piece by piece as needed.

If you want to append to a file, you should use File.AppendText which will create a StreamWriter that appends to the end of a file.

Here is an example:

 string path = @"c:\temp\MyTest.txt";

 // This text is always added, making the file longer over time
 // if it is not deleted.
 using (StreamWriter sw = File.AppendText(path)) 
 {
     sw.WriteLine("This");
     sw.WriteLine("is Extra");
     sw.WriteLine("Text");
 } 

Again, the whole file will not be stored in memory.

Documentation: http://msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx


The .NET FileStream will buffer a small amount of data (you can set this amount with some of the constructors).

The Windows OS will do more significant caching of the file, if you have plenty of RAM this might be the whole file.


A StreamReader uses FileStream to open the file. FileStream stores a Windows handle, returned by the CreateFile() API function. It is 4 bytes on a 32-bit operating system. FileStream also has a byte[] buffer, it is 4096 bytes by default. This buffer avoids having to call the ReadFile() API function for every single read call. StreamReader itself has a small buffer to make decoding the text in the file more efficient, it is 128 bytes by default. And it has some private variables to keep track of the buffer index and whether or not a BOM has been detected.

This all adds up to a few kilobytes. The data you read with StreamReader will of course take space in your program's heap. That could add up to 12 megabytes if you store every string in, say, a List. You usually want to avoid that.


StreamReader will not read the 6 MB file into memory. Also, you can't append a line to the end of the file using StreamReader. You might want to use StreamWriter.

update: not counting buffering and OS caching as someone else mentioned

0

精彩评论

暂无评论...
验证码 换一张
取 消