I have some very large files of size 500MB++containing integer values (actually this is a bit more complicated), I read these files in a loop and calculate the maximum value for all files. For some reason, memory grows constantly during processing, it seems that GC never releases memory received by previous instances lines.
I can not transfer data and use GetFileLinesfor each file. If the actual amount of memory required for storage linesfor a single file is 500MB, why am I getting 5GBfrom RAM, which is used after processing 10 files? In the end, it crashes out of memory after 15 files.
Payment:
int max = int.MinValue;
for (int i = 0; i < 10; i++)
{
IEnumerable<string> lines = Db.GetFileLines(i);
max = Math.Max(max, lines.Max(t=>int.Parse(t)));
}
GetFileLines Code:
public static List<string> GetFileLines(int i)
{
string path = GetPath(i);
List<string> lines = new List<string>();
string line;
using (StreamReader reader = File.OpenText(path))
{
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
reader.Close();
reader.Dispose();
}
return lines;
}