Read Big TXT File, Out of Memory Exception

Just use File.ReadLines which returns an IEnumerable<string> and doesn’t load all the lines at once to the memory.

foreach (var line in File.ReadLines(_filePath))
{
    //Don't put "line" into a list or collection.
    //Just make your processing on it.
}

Leave a Comment