找回密码
 FreeOZ用户注册
查看: 1682|回复: 9
打印 上一主题 下一主题

[职业发展] 各位IT大虾,求助

[复制链接]
跳转到指定楼层
1#
发表于 9-11-2007 13:26:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
要读入400多MB的文本文件,因为涉及到encoding的问题,要通过StreamReader来读入(.NET),在开辟buffer时候,抛出out of memory异常,因为物理内存不够。如何在开辟buffer的时候使用虚拟内存,是否可以通过调用API来实现?谢谢
回复  

使用道具 举报

2#
发表于 9-11-2007 14:14:17 | 只看该作者
我不熟.Net,粗略建议

1。不要用buffer一次全部装载整个文件,用low level IO API,File inputstream之类的,一个字节一个字节的读

2。增加进程的内存参数(虚拟内存是OS管理的,不需要application指定)

3。调用IO前显式请求垃圾回收,释放内存
回复  

使用道具 举报

3#
 楼主| 发表于 9-11-2007 14:50:48 | 只看该作者
谢谢ls

我要处理的数据是Big5 encoding,如果用byte stream,那么这个encoding的信息无法保存,只能用16bits的char来保存,在.NET下,char是16bits的unicode,所以只能选择StreamReader,因为只有这个才可以指定encoding。

问题关键还是buffer,如果不一次装载整个文件,那么这个buffer开多大比较合适?因为这个文件是corpus collection,无法确定每个corpus的长度,而且分批次读,还涉及索引以及效率问题。
回复  

使用道具 举报

4#
发表于 9-11-2007 18:23:30 | 只看该作者
My 2 cents

1. The data file does not contain encoding info, so there is no such a thing called "Loosing the encoding info".

2. Virtual memory is managed by OS, you are not supposed to touch it.

3. Set the buffer lower, bufferred stream will load the data from the file system to memory buffer automatically. 10M buffer is enough for a 400M file.

4. Paste you code here so that we can know what you are doing exactly....

[ 本帖最后由 Coolioo 于 9-11-2007 18:30 编辑 ]
回复  

使用道具 举报

5#
发表于 9-11-2007 18:30:22 | 只看该作者
one Big5 character is fixed 16 bits, you can read two bytes from a stream and convert it to a big5 character...

But the better practice is to use StreamReader with Big5 encoding....
回复  

使用道具 举报

6#
发表于 9-11-2007 19:11:37 | 只看该作者
There is one thing in computer is '\0', this means end of a line.
Just read in one line each time till EOF(end of file) and decode it.
回复  

使用道具 举报

7#
发表于 9-11-2007 20:01:32 | 只看该作者
\0 only exits in C/C++, C# and Java has no such a concept
回复  

使用道具 举报

8#
发表于 9-11-2007 20:15:52 | 只看该作者
C# Code

// --- Write a 400M file containing one big5 character 陸 ---
StreamWriter bigWriter = new StreamWriter(@"big5.txt", false, Encoding.GetEncoding("big5"));
for (int i = 0; i < 209715200; i++) bigWriter.Write("陸");
bigWriter.Close();
Console.WriteLine("Writing Done");

// --- Read big5 characters from the 400M file  ---
StreamReader bigReader = new StreamReader(@"big5.txt", Encoding.GetEncoding("big5"));
// - print out the first big character -
char[] chars = new char[1];
bigReader.Read(chars, 0, 1);
Console.WriteLine(chars);
// - Read the rest and print the total number of characters read -
int counter = 1;
while (-1 != bigReader.Read()) counter++;
bigReader.Close();
Console.WriteLine("Reading Done: " + counter);

[ 本帖最后由 Coolioo 于 9-11-2007 20:18 编辑 ]
回复  

使用道具 举报

9#
发表于 9-11-2007 20:21:32 | 只看该作者
牛啊!!!
回复  

使用道具 举报

10#
 楼主| 发表于 11-11-2007 14:47:10 | 只看该作者
问题已经解决了,感谢ls几位大虾。
回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+10, 8-8-2026 06:08 , Processed in 0.014777 second(s), 26 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表