问题描述
我有一个Windows服务试图解析一个大的(> 1Gig)文本
文件。我一直得到OutOfMemoryException异常。这是有问题的
代码:
使用(StreamReader streamReader = new
StreamReader(流,编码) .ASCII))
{
string line ="" ;;
DateTime currentDate = DateTime.Now.Date;
while(streamReader.Peek()> -1)
{
line = streamReader.ReadLine();
}
}
我阅读文档并意识到ReadLine()方法并不是每个有效的
。还有另外一种方法可以做到这一点吗?
谢谢,
Hai
I have a Windows Service that is trying to parse a large (> 1Gig) text
file. I am keep getting OutOfMemoryException exception. Here is the
code that''s having problem:
using (StreamReader streamReader = new
StreamReader(stream,Encoding.ASCII))
{
string line = "";
DateTime currentDate = DateTime.Now.Date;
while (streamReader.Peek() > -1)
{
line = streamReader.ReadLine();
}
}
I read the documentation and realized that the ReadLine() method is not
every efficient. Is there another way that I can do this?
Thanks,
Hai
推荐答案
不,这不是OOM的原因,Brendan是正确到位。
此外,只要GC
达到gen0阈值(大小在256KB到几MB之间),您的描述就无法反映GC的工作原理CLR
将劫持当前线程并强制GC收集,没有任何东西可以阻止
这种情况发生。别忘了应用程序必须输入CLR
来实例化一个新对象,那时CLR检查GC堆
统计并决定启动一个当触发点时的GC动作。满足了。
威利。
No, this is not the reason for the OOM, Brendan is right to the point.
Also, your description does not reflect how the GC works, whenever the GC
reaches the gen0 threshold ( sizes vary between 256KB and a few MB) the CLR
will hijack the current thread and force a GC collection, nothing can stop
this from happening. Don''t forget that an application has to enter the CLR
to instantiate a new object, at that time the CLR inspects the GC heap
statistics and decides to start a GC action when a "trigger point" is met.
Willy.
这篇关于读取大文本文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!