我目前正在为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
答案 0 :(得分:2)
Mikurski,
我立即看到了一个问题。你是Dim'ing Stream读者。您应该将其包含在使用块中,或者确保在每个代码路径的末尾都执行.Dispose
。任何实现IDisposable接口的东西都应该在using块中处理或使用,如下所示:
Using fs As New FileStream("C:\testing.txt", FileMode.Open)
Using sr As StreamReader = New StreamReader(fs)
Dim message As String = sr.ReadLine()
MessageBox.Show(message)
End Using
End Using
我希望这有帮助,
编辑:
以下是实现您的代码未处理的IDisposable的项目:
希望这有帮助,
这是一个内存密集度较低的函数:
Function LoadFixedWidthFileToDataTable(ByVal filepath As String, ByRef Colnames() As String, ByRef colwidth() As Integer) As DataTable
Dim filetable As New DataTable
Try
For Each name As String In Colnames
filetable.Columns.Add(name)
Next
Try
Using loadedfile As StreamReader = New StreamReader(filepath)
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
Exit For
End Try
i = i + 1
Next
filetable.Rows.Add(filerow)
End While
loadedfile.Close()
End Using
Catch io As IOException
MsgBox(io.Message)
Return Nothing
End Try
Return filetable
Catch ex As Exception
filetable.Dispose()
End Try
Return Nothing
End Function
我注意到你要归还你应该处理的东西。如果你这样做,你不能在这里处理它。相反,调用此函数的位置必须处理它。如果你围绕这个包装一个使用块,你将遇到问题,因为在你有机会从调用代码对它进行操作之前,将丢弃返回的对象。
希望这有帮助,