Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why might ReadToEnd throw an OutOfMemory exception but ReadAllText doesn't?

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)

like image 530
thelsdj Avatar asked Jun 16 '11 22:06

thelsdj


People also ask

How can OutOfMemory exception be prevented?

To avoid this exception while working with StringBuilder, we can call the constructor StringBuilder. StringBuilder(Int32, Int32) and can set the MaxCapacity property to a value that will be large enough to serve the accommodation required when we expand the corresponding StringBuilder object.

How do you fix exception of type system OutOfMemoryException was thrown?

OutOfMemoryException Exception of type 'System. OutOfMemoryException' was thrown. To resolve this issue, I had to restart Visual Studio or go to the Windows Task Manager and terminate IIS Express process. This error could happen due to a variety of reasons related to memory consumption of the application.

What is exception of type system OutOfMemoryException was thrown?

An OutOfMemoryException exception has two major causes: You are attempting to expand a StringBuilder object beyond the length defined by its StringBuilder. MaxCapacity property. The common language runtime cannot allocate enough contiguous memory to successfully perform an operation.


1 Answers

From Reflector, System.IO.File class:

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

Just like that.

like image 139
Ivan Danilov Avatar answered Oct 28 '22 22:10

Ivan Danilov