【发布时间】:2012-05-16 10:40:45
【问题描述】:
我目前正在为 VB 中的固定宽度表格编写文件阅读器,编译后的应用程序似乎正在耗尽内存,就像没有明天一样。我正在处理一系列约 50 兆字节的文件,但在运行了几个文件后,该进程开始占用大约 200 多兆字节的 RAM,这远远超出了应有的范围。
我做了一些探索,我认为问题在于对 NewRow() 的调用,但不要相信我的话。
有没有人有一些优化这个的技巧?如果问题出在 NewRow() 调用上,有没有办法解决这个问题?
代码如下:
Function LoadFixedWidthFileToDataTable(ByVal filepath As String, ByRef Colnames() As String, ByRef colwidth() As Integer) As DataTable
Dim filetable As New DataTable
For Each name As String In Colnames
filetable.Columns.Add(name)
Next
Dim loadedfile As StreamReader
Try
loadedfile = New StreamReader(filepath)
Catch io As IOException
MsgBox(io.Message)
Return Nothing
Exit Function
End Try
Dim line As String = loadedfile.ReadLine
Dim filerow As DataRow = filetable.NewRow
Dim i As Integer = 0
While Not loadedfile.EndOfStream
line = loadedfile.ReadLine
filerow = filetable.NewRow
i = 0
For Each colsize As Integer In colwidth
Try
filerow(i) = line.Substring(0, colsize)
line = line.Remove(0, colsize)
Catch ex As ArgumentOutOfRangeException ''If the line doesn't match array params
Exit For
End Try
i = i + 1
Next
filetable.Rows.Add(filerow)
End While
loadedfile.Close()
Return filetable
End Function
【问题讨论】:
标签: vb.net memory-leaks