Given this code:
readFile = new FileStream(FilePath, FileMode.Open);
streamReader = new StreamReader(readFile);
dsSheet = new DataSet();
dsSheet.Tables.Add(sValidNumbersLibrary);
dsSheet.Tables[sValidNumbersLibrary].Columns.Add("Numbers");
dt50Records = dsSheet.Tables[sValidNumbersLibrary].Clone();
String sLine = string.Empty;
sLine = streamReader.ReadToEnd();
// The next line fails with System.OutOfMemoryException:
sLine = (sLine.Contains(",")) ? sLine.Replace(",", "\r\n") : sLine;
sLine = sLine.Replace("\r\n", ",");
How should I handle this System.OutOfMemoryException?
What are you planning to do with the altered string once you’re done with your manipulation? If you are just going to write it to a file, then perhaps you should read one line at a time, perform your manipulation, then write the altered line to the file. StreamReader’s readline will return everything until it encounters \r\n. Since you are planning on replacing \r\n with “,”, you can just append a comma to the string returned by readline.
If you are planning on using your manipulated text throughout the rest of your code, then you can build the string with StringBuilder (replace sw.Write with sb.Append); however, using StringBuilder will not guarantee that you won’t run into an OutOfMemoryException. A large string is a large string. If there’s no contiguous block of memory for your string, then you will get that OutOfMemoryException. Also, as @SteveCzetty pointed out, your calls to string.Replace() returns a copy of your string. The larger the string, the more likely you’re going to encounter the exception.