I was instructed to process a text file with a fixed width of 3.2 GB. Each line has a length of 1563 characters, and in a text file about 2.1 million lines. After reading about 1 million lines, my program crashes due to a memory exception error.
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Module TestFileCount
''' <summary>
''' Gets the total number of lines in a text file by reading a line at a time
''' </summary>
''' <remarks>Crashes when count reaches 1018890</remarks>
Sub Main()
Dim inputfile As String = "C:\Split\BIGFILE.txt"
Dim count As Int32 = 0
Dim lineoftext As String = ""
If File.Exists(inputfile) Then
Dim _read As New StreamReader(inputfile)
Try
While (_read.Peek <> -1)
lineoftext = _read.ReadLine()
count += 1
End While
Console.WriteLine("Total Lines in " & inputfile & ": " & count)
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
_read.Close()
End Try
End If
End Sub
End Module
This is a fairly simple program that reads a text file one line at a time, so I assume that it should not take up too much memory in the buffer.
In my life, I cannot understand why it is crumbling. Does anyone have any ideas?
source
share