简体   繁体   English

文件打开内存C#

[英]File open memory c#

While opening a file in C# using stream reader is the file going to remain in memory till it closed. 使用流阅读器在C#中打开文件时,文件将保留在内存中,直到关闭为止。 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. 例如,如果程序使用streamreader打开了大小为6MB的文件,并在文件末尾附加了一行。 Will the program hold the entire 6 MB in it's memory till file is closed. 程序将整个6 MB的内容保存在其内存中,直到关闭文件。 OR is a file pointer returned internally by .Net code and the line is appended at the end. OR是.Net代码在内部返回的文件指针,并且该行附加在末尾。 So the 6MB memory will not be taken up by the program 因此程序不会占用6MB的内存

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. 如果要追加到文件,则应使用File.AppendText ,这将创建一个StreamWriter追加到文件的末尾。

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 文档: http : //msdn.microsoft.com/zh-cn/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). .NET FileStream将缓冲少量数据(您可以使用某些构造函数设置此数量)。

The Windows OS will do more significant caching of the file, if you have plenty of RAM this might be the whole file. Windows OS将对文件进行更重要的缓存,如果您有足够的RAM,则可能是整个文件。

StreamReader will not read the 6 MB file into memory. StreamReader 不会将6 MB的文件读入内存。 Also, you can't append a line to the end of the file using StreamReader. 同样,您不能使用StreamReader在文件末尾添加一行。 You might want to use StreamWriter. 您可能要使用StreamWriter。

update: not counting buffering and OS caching as someone else mentioned 更新:不像其他人所说的那样计算缓冲和操作系统缓存

A StreamReader uses FileStream to open the file. StreamReader使用FileStream打开文件。 FileStream stores a Windows handle, returned by the CreateFile() API function. FileStream存储Windows句柄,由CreateFile()API函数返回。 It is 4 bytes on a 32-bit operating system. 在32位操作系统上,它是4个字节。 FileStream also has a byte[] buffer, it is 4096 bytes by default. FileStream还具有一个byte []缓冲区,默认情况下为4096字节。 This buffer avoids having to call the ReadFile() API function for every single read call. 该缓冲区避免了每次读取调用都必须调用ReadFile()API函数。 StreamReader itself has a small buffer to make decoding the text in the file more efficient, it is 128 bytes by default. StreamReader本身具有一个小的缓冲区,可以使文件中的文本解码效率更高,默认为128字节。 And it has some private variables to keep track of the buffer index and whether or not a BOM has been detected. 并且它具有一些私有变量来跟踪缓冲区索引以及是否已检测到BOM。

This all adds up to a few kilobytes. 所有这些加起来只有几千字节。 The data you read with StreamReader will of course take space in your program's heap. 用StreamReader读取的数据当然会占用程序堆中的空间。 That could add up to 12 megabytes if you store every string in, say, a List. 如果将每个字符串存储在一个列表中,则总计可能多达12兆字节。 You usually want to avoid that. 您通常想避免这种情况。

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

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