Garbage collection may be a solution. I am not sure if this is a problem. But if this is the case, a simple GC.Collect is often insufficient and, for performance reasons, should be called if it is really necessary. Try the following procedure, which causes garbage when available memory is too low (below the threshold provided as a procedure parameter).
int charReadSinceLastMemCheck = 0 ; using (var streamReader = File.OpenText(_filePath)) { int lineNumber = 1; string currentString = String.Empty; while ((currentString = streamReader.ReadLine()) != null) { ProcessString(currentString, lineNumber); Console.WriteLine("Line {0}", lineNumber); lineNumber++; totalRead+=currentString.Length ; if (charReadSinceLastMemCheck>1000000) { // Check memory left every Mb read, and collect garbage if required CollectGarbage(100) ; charReadSinceLastMemCheck=0 ; } } } internal static void CollectGarbage(int SizeToAllocateInMo) { long [,] TheArray ; try { TheArray =new long[SizeToAllocateInMo,125000]; }low function catch { TheArray=null ; GC.Collect() ; GC.WaitForPendingFinalizers() ; GC.Collect() ; } TheArray=null ; }
source share