Search code examples
c#csvfile.readalllines

Why does ReadAllLines work in WPF but not in ConsoleApp


I am doing some benchmarking on different ways to read a CSV file and found a "wierd" problem. The problem being that when I use this method in a console application:

        var lines = File.ReadAllLines(fileName); // OutOfMemoryException
        foreach (var line in lines)
        {
            //doing stuff
        }

I get an OutOfMemoryException, but when I use the same method in my WPF project it works fine. The file im testing this on is 730MB and I know not to use ReadAllLines on bigger CSV files but why does this method work in the WPF application but not in the console application?


Solution

  • You're using a 32 bit process and suffering from address space fragmentation. This means that the runtime cannot allocate enough contiguous memory, although in total, enough memory would be free. WPF might just tend to have a better memory layout.

    Change the project to be 64 bit.

    Or, stream the file using File.ReadLines.