将(2~3兆字节)二进制文件转换为其他格式时出现虚拟内存异常错误。 此错误仅发生在具有100 MB虚拟内存的低性能PC上。 Performance report in realtime (.net Memory profiler)
这是发生内存使用量增加的代码:
Using reader As BinaryReader = New BinaryReader(New FileStream(strSrcFileName, FileMode.Open))
Dim BuffOffset As Integer 'offset of reading the DAT file every time
Dim RecSize As Integer 'no of records to process each time
While intRecordsCount > 0 'reader.BaseStream.Position <> reader.BaseStream.Length
If intRecordsCount > RecordsChunck Then
RecSize = RecordsChunck
intRecordsCount -= RecordsChunck
Else
RecSize = intRecordsCount
intRecordsCount = 0
End If
Dim tempBuffer(RecSize * intRecordLength - 1) As Byte
Dim outputbuffer(RecSize * (intRecordLength - 8) - 1) As Byte
reader.BaseStream.Seek(BuffOffset, SeekOrigin.Begin)
reader.Read(tempBuffer, 0, tempBuffer.Length)
BuffOffset += tempBuffer.Length 'prepare the offset for the next reading
'conversion is done here
'append the converted buffer into the target binary file
Using writer As BinaryWriter = New BinaryWriter(File.Open(strTargetFileName, FileMode.Append))
writer.Write(outputbuffer)
End Using
End While
End Using
在附加目标文件(编写器流)
时,会按预期发生增加我的问题是,在加载源文件和目标文件时,我可以做些什么来避免(大量)内存使用情况,以及在这种情况下最佳做法是什么?