简体   繁体   English

反复读取小文件会抛出OutOfMemory

[英]Repeatedly reading small file throws OutOfMemory

I have two programs that work together. 我有两个程序可以一起工作。 To coordinate their operations I use a small settings file. 为了协调他们的操作,我使用了一个小的设置文件。 This setting file contains two words separated by a ';'. 该设置文件包含两个单词,中间用“;”分隔。 So in the one program I repeatedly read the words in the file using a while loop. 因此,在一个程序中,我使用while循环反复读取文件中的单词。 By repeatedly I mean once every second. 重复是指每秒一次。 The loop only terminates when the program terminates; 循环仅在程序终止时终止;否则,程序终止。 when the user turns off the pc. 当用户关闭电脑时。

But with each iteration of the loop the program size in memory increases until the program throws an OutOfMemory exception. 但是随着循环的每次迭代,内存中的程序大小都会增加,直到程序抛出OutOfMemory异常。 I have tried two different methods of reading the files but both causes the program to 'grow' in memory. 我尝试了两种不同的读取文件的方法,但是都使程序在内存中“增长”。

FileStream FS = new FileStream("br.stat", FileMode.Open);
StreamReader SR = new StreamReader(FS);
string s = SR.ReadToEnd();
FS.Dispose();
SR.Dispose();

and

string S = File.ReadAllText("br.stat");

Is there a way to read a file repeatedly with out this happening? 有没有一种方法可以重复读取文件而不会发生这种情况?

Thanks. 谢谢。

the problem is the design, more so than the implementation. 问题是设计,而不是实施。 You only need to read from the file once when the app starts and again when the file changes. 您只需要在应用启动时从文件中读取一次,以及在文件更改时从文件中读取一次。 You can use FileSystemWatcher to detect changes to the file and reload the settings. 您可以使用FileSystemWatcher来检测对文件的更改并重新加载设置。

This uses drastically less resources than reading the file indefinitely. 与无限期读取文件相比,这将大大减少资源。

also, you'll want to take advantage of the using keyword to ensure you properly dispose of the file stream and reader. 另外,您将希望利用using关键字来确保正确处理文件流和阅读器。 In fact I would simplify and just use File.ReadAllText(filename) . 实际上,我会简化并只使用File.ReadAllText(filename)

I think you're doing it wrong. 我认为您做错了。 Realistically there is no need to read the file every iteration unless its changed. 实际上,除非更改,否则无需每次读取文件。

Instead it would be better to use a FileSystemWatcher http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed(v=vs.85).aspx to read the values and change your control values. 相反,最好使用FileSystemWatcher http://msdn.microsoft.com/zh-cn/library/system.io.filesystemwatcher.changed(v=vs.85).aspx读取值并更改控件值。

You should also use the following to circumvent your memory leak issues 您还应该使用以下内容来避免内存泄漏问题

using (var FS = new FileStream("br.stat", FileMode.Open))
{
    using (var SR = new StreamReader(FS))
    {
        var s = SR.ReadToEnd();
    }
}

I would recomend you look at using an using statement 我建议您使用using语句

File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). 文件和字体是访问非托管资源(在这种情况下,文件句柄和设备上下文)的托管类型的示例。 There are many other kinds of unmanaged resources and class library types that encapsulate them. 还有许多其他类型的非托管资源和封装它们的类库类型。 All such types must implement the IDisposable interface. 所有此类类型都必须实现IDisposable接口。

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. 通常,使用IDisposable对象时,应在using语句中声明并实例化它。 The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. using语句以正确的方式在对象上调用Dispose方法,并且(如前所述,当您使用它时)它还会导致对象本身在调用Dispose时就超出范围。 Within the using block, the object is read-only and cannot be modified or reassigned. 在using块中,该对象是只读的,无法修改或重新分配。

so something like 所以像

using (FileStream FS = new FileStream("br.stat", FileMode.Open))
using (StreamReader SR = new StreamReader(FS))
{
    string s = SR.ReadToEnd();
}

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

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