I am creating a tool which analyze the data quality of a file. So i need to read each line of the file and analyze each one of them. I also need to store in memory all the lines of my file as the user will be able to drill down to specific sections. So basically all works fine for a file containing thousands of rows. However when trying with a CSV file containing more than 4 millions rows i m getting an out of memory exception. I thought C# would be able to handle a few millions of data in its memory cache but does not seem like it. So i am a bit stuck and dont know what to do. Maybe my piece of code is not the most performant so if you can tell me a way to improve it that would be great? Just to keep in mind that i need to have all lines of the file in memory because depending of the user's action i need to access speciifc lines to display them to the user.
Below is the call which reads every single line
using (FileStream fs = File.Open(this.dlgInput.FileName.ToString(), FileMode.Open, FileAccess.Read, FileShare.Read))
using (BufferedStream bs = new BufferedStream(fs))
using (System.IO.StreamReader sr = new StreamReader(this.dlgInput.FileName.ToString(), Encoding.Default, false, 8192))
{
string line;
if (this.chkSkipHeader.Checked)
{
sr.ReadLine();
}
progressBar1.Visible = true;
int nbOfLines = File.ReadLines(this.dlgInput.FileName.ToString()).Count();
progressBar1.Maximum = nbOfLines;
this.lines = new string[nbOfLines][];
this.patternedLines = new string[nbOfLines][];
for (int i = 0; i < nbOfLines; i++)
{
this.lines[i] = new string[this.dgvFields.Rows.Count];
this.patternedLines[i] = new string[this.dgvFields.Rows.Count];
}
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
this.recordCount += 1;
char[] c = new char[1] { ',' };
System.Text.RegularExpressions.Regex CSVParser = new System.Text.RegularExpressions.Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
String[] fields = CSVParser.Split(line);
ParseLine(fields);
this.lines[recordCount - 1] = fields;
progressBar1.PerformStep();
}
}
And below is the ParseLine function which also keeps in memory via arrays some analysis needed:
private void ParseLine(String[] fields2)
{
for (int j = 0; j <= fields2.Length - 1; j++)
{
if ((int)this.dgvFields.Rows[j].Cells["colSelected"].Value == 1)
{
/*' ************************************************
' Save Number of Counts by Value
' ************************************************/
if (this.values[j].ContainsKey(fields2[j]))
{
//values[0] = Dictionary<"TEST", 1> (fields2[0 which is source code] = count])
this.values[j][fields2[j]] += 1;
}
else
{
this.values[j].Add(fields2[j], 1);
}
/* ' ************************************************
' Save Pattern Values/Counts
' ************************************************/
string tmp = System.Text.RegularExpressions.Regex.Replace(fields2[j], "\\p{Lu}", "X");
tmp = System.Text.RegularExpressions.Regex.Replace(tmp, "\\p{Ll}", "x");
tmp = System.Text.RegularExpressions.Regex.Replace(tmp, "[0-9]", "0");
if (this.patterns[j].ContainsKey(tmp))
{
this.patterns[j][tmp] += 1;
}
else
{
this.patterns[j].Add(tmp, 1);
}
this.patternedLines[this.recordCount - 1][j] = tmp;
/* ' ************************************************
' Count Blanks/Alpha/Numeric/Phone/Other
' ************************************************/
if (String.IsNullOrWhiteSpace(fields2[j]))
{
this.blanks[j] += 1;
}
else if (System.Text.RegularExpressions.Regex.IsMatch(fields2[j], "^[0-9]+$"))
{
this.numeric[j] += 1;
}
else if (System.Text.RegularExpressions.Regex.IsMatch(fields2[j].ToUpper().Replace("EXTENSION", "").Replace("EXT", "").Replace("X", ""), "^[0-9()\\- ]+$"))
{
this.phone[j] += 1;
}
else if (System.Text.RegularExpressions.Regex.IsMatch(fields2[j], "^[a-zA-Z ]+$"))
{
this.alpha[j] += 1;
}
else
{
this.other[j] += 1;
}
if (this.recordCount == 1)
{
this.high[j] = fields2[j];
this.low[j] = fields2[j];
}
else
{
if (fields2[j].CompareTo(this.high[j]) > 0)
{
this.high[j] = fields2[j];
}
if (fields2[j].CompareTo(this.low[j]) < 0)
{
this.low[j] = fields2[j];
}
}
}
}
}
UPDATE: new piece of code
int nbOfLines = File.ReadLines(this.dlgInput.FileName.ToString()).Count();
//Read file
using (System.IO.StreamReader sr = new StreamReader(this.dlgInput.FileName.ToString(), Encoding.Default, false, 8192))
{
string line;
if (this.chkSkipHeader.Checked)
{ sr.ReadLine(); }
progressBar1.Visible = true;
progressBar1.Maximum = nbOfLines;
this.lines = new string[nbOfLines][];
this.patternedLines = new string[nbOfLines][];
for (int i = 0; i < nbOfLines; i++)
{
this.lines[i] = new string[this.dgvFields.Rows.Count];
this.patternedLines[i] = new string[this.dgvFields.Rows.Count];
}
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
this.recordCount += 1;
char[] c = new char[1] { ',' };
System.Text.RegularExpressions.Regex CSVParser = new System.Text.RegularExpressions.Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
String[] fields = CSVParser.Split(line);
ParseLine(fields);
this.lines[recordCount - 1] = fields;
progressBar1.PerformStep();
}
}