Home > OS >  My vb.net app read txt file and got "out of memory" error sometime
My vb.net app read txt file and got "out of memory" error sometime

Time:09-27

My app is reading text files (only a few line, text file size is around 60 GB, as the test total file in the directory is 53 files ) from the directory and loops all files. I wondering if there is any management need to process close or do something with it after reading?. concern part of the code is below

For Each i As String In ListBox1.Items
            TextBox2.Text = TextBox1.Text & "\" & (i).ToString
            Dim lineCount = IO.File.ReadLines(TextBox2.Text).Count()
            Dim Counter As String = System.IO.File.ReadAllLines(TextBox2.Text)(lineCount - 2)
            TextBox3.Text = Counter
            DataGridView1.Rows.Add((i).ToString, System.IO.File.ReadAllLines(TextBox2.Text)(11), "Back Up", "Complete", "100%", System.IO.File.ReadAllLines(TextBox2.Text)(5), Counter, System.IO.File.ReadAllLines(TextBox2.Text)(lineCount - 7).Substring(0, 21), System.IO.File.ReadAllLines(TextBox2.Text)(lineCount - 8).Substring(0, 21))

Or if anybody has any suggestion it would be appreciated.

CodePudding user response:

One issue is that you're calling ReadAllLines an astounding six times:

  1. Once to get the Count
  2. Once to get the second to last line
  3. Once to add the 12th line to the DataGridView
  4. Once to add the 6th line to the DataGridView
  5. Once to add the 8th to last line to the DataGridView
  6. Once to add the 9th to last line to the DataGridView

This right here is a code smell. My first suggestion is to call the method only once, store the result in a variable, then reference it when needed.

I would also suggest using some data validation. What if you try to access a line that doesn't exist or if you try to grab part of the line that doesn't exist? Right now it'd throw an exception.

Finally, in my example I am splitting the cell values up. This just makes it easier to read/debug in my opinion. However, if you want them to all be in one line like you have it, then that's fine.

TextBox2.Text = IO.Path.Combine(TextBox1.Text, i.ToString())
Dim lines = IO.File.ReadLines(TextBox2.Text)
Dim lineCount = lines.Length
Dim counter As String = If(lineCount > 2, lines(lineCount - 2), String.Empty)
TextBox3.Text = counter

Dim cell1 = i.ToString()
Dim cell2 = If(lines.Length > 11, lines(11), String.Empty)
Dim cell3 = "Back Up"
Dim cell4 = "Complete"
Dim cell5 = "100%"
Dim cell6 = If(lines.Length > 5, lines(5), String.Empty)
Dim cell7 = counter
Dim cell8 = If(lines.Length > lineCount - 7, lines(lineCount - 7), String.Empty)
If (cell8.Length > 20) Then
    cell8 = cell8.Substring(0, 21)
End If
Dim cell9 = If(lines.Length > lineCount - 8, lines(lineCount - 8, String.Empty)
If (cell9.Length > 20) Then
    cell9 = cell9.Substring(0, 21)
End If
DataGridView1.Rows.Add(cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9)

If performance is still an issue, then you can modify the method to use the ReadLines method instead. This will not cause the code to read the entire file in one go. Per the documentation:

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned. When you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

  • Related