为什么ReadToEnd会抛出OutOfMemory异常但ReadAllText却没有?

the*_*sdj 5 .net c# file-io out-of-memory

我遇到一个问题,ReadToEnd在尝试读取ASP.net网站中的16MB文本文件时抛出OutOfMemory异常.

在调查原因的时候,我遇到了File.ReadAllText,这正是我正在做的事情,我并不关心如何获取文本.

但是看看ReadAllText的文档,它没有提到OutOfMemory异常的可能性.这是为什么?它的实现方式与ReadToEnd的不同之处在于它不太可能耗尽内存,或者如果内存不足会引发其他异常吗?

编辑添加代码只是为了显示我目前正在做的事情:

StreamReader inputFile = System.IO.File.OpenText(filename);
string cacheData = inputFile.ReadToEnd();
inputFile.Close();
Run Code Online (Sandbox Code Playgroud)

有时我在第2行得到了OutOfMemory异常.没有进行解析,文件只有16M的文本,我知道并不奇怪.

重新启动IIS通常会修复它.但是当我收到错误时,我有2G的RAM可用,IIS可能会达到内部限制吗?w3wp.exe进程通常使用350-500M(这是Windows Server 2003上的IIS 6)

Iva*_*lov 6

从Reflector,System.IO.File类:

public static string ReadAllText(string path, Encoding encoding)
{
    using (StreamReader reader = new StreamReader(path, encoding))
    {
        return reader.ReadToEnd();
    }
}
Run Code Online (Sandbox Code Playgroud)

就像那样.