I’m running into an issue where ReadToEnd is throwing an OutOfMemory exception when trying to read a 16MB text file in an ASP.net website.
While investigating the cause I came across File.ReadAllText which is really what I’m doing, I don’t care about how I get the text.
But looking at the documentation of ReadAllText it doesn’t mention the possibility of an OutOfMemory exception. Why is that? Is it implemented differently than ReadToEnd in such a way that its less likely to run out of memory, or does it throw some other exception if it runs out of memory?
Edit Adding code just to show what I’m currently doing:
StreamReader inputFile = System.IO.File.OpenText(filename);
string cacheData = inputFile.ReadToEnd();
inputFile.Close();
And sometimes I get OutOfMemory exception on line 2. No parsing going on, the file is only 16M of text, nothing strange that I know of.
Restarting IIS generally fixes it. But I have like 2G of RAM free when I get the error, IIS maybe hitting some internal limit? The w3wp.exe process usually uses 350-500M (This is IIS 6 on Windows Server 2003)
From Reflector, System.IO.File class:
Just like that.