1

StreamReader在一个大文本文件(~300 MB)上使用 a 并在循环中调用 ReadLine() 最终会引发一个奇怪的问题ArgumentOutOfRangeException

Index and count must refer to a location within the buffer.
Parameter name: bytes

堆栈跟踪是:

at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount,
Char[] chars, Int32 charIndex, Boolean flush)
at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount,
Char[] chars, Int32 charIndex)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()
....

这是一些奇怪的记忆错误吗?它不会抛出OutOfMemoryException,所以我有点迷路了。它发生在第 2000 行左右。到那时,即使它继续执行成功的 ReadLine(),它也会开始在其他地方抛出奇怪的异常。因此,某处可能存在内存泄漏,但是有什么特定的ReadLine()StreamReader需要查看的吗?

我的文本文件中的每一行正好是 24 个字符长。

4

1 回答 1

1

如果问题特定于 StremReader.ReadLine() 它可能是:

  • 下一行的字符数大于 Int32.MaxValue
  • 您传递给 StremReader 构造函数的缓冲区太小
于 2013-03-05T10:47:55.073 回答