看板 C_Sharp 關於我們 聯絡資訊
不好意思,我所處理的IO文字檔是未格式化的 文字檔最少有1GB多,差不多有2千多萬行 我用ReadAllLines方式存在string變數裡 但執行確出現"OutOfMemoryException" 不知道有沒有高手可以幫忙~"~ 感謝萬分 DirectoryInfo di = new DirectoryInfo(System.Environment.CurrentDirectory + @"\ tm List<String> _sub = new List<string>(); //宣告動態陣列 foreach (var fi in di.GetFiles("*", SearchOption.AllDirectories)) { //tmp資料夾裡的文字檔都做處理 Console.WriteLine("準備中....."); Console.WriteLine(System.Environment.NewLine); foreach (string s in System.IO.File.ReadAllLines(di + fi.Name)) {//將檔案內容放置_sub陣列裡 _sub.Add(s); counter++; } } P.S: 光一個文字檔就有1GB多了;資料量非常龐大... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.231.50.253 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1513643689.A.CCD.html
ssccg: 大檔案不要一次全讀進記憶體啊12/19 09:09
ssccg: 用Stream一次處理一部分就好12/19 09:10
testPtt: StreamReader 有試過嗎12/19 09:13
不好意思,我用StreamReader也一樣 StreamReader sr =.... while(sr.ReadLine()!=null) { } 在六百多萬行就出現outofmemory ※ 編輯: takesi5200 (42.76.110.203), 12/19/2017 10:51:56
Laluth: 應該是卡到執行檔使用記憶體的上限 程式改成64bit看看 12/19 13:04
Litfal: 不是阿,你讀完放到陣列裡面,不就等於放在記憶體裡 12/19 13:59
Litfal: 你文字檔格式是什麼? 12/19 13:59
Litfal: UTF-8或ANSI讀出來成String變Unicode,單位元組字多的話, 12/19 14:02
Litfal: 讀出來需要的記憶體就翻倍了。1GB檔讀完爆2GB記憶體很正常 12/19 14:03
Litfal: 你可以試試用.nf 4.5以上的64bit,或是改寫你每一行處理的 12/19 14:05
Litfal: 邏輯 12/19 14:05
capercat: 搜尋 gcAllowVeryLargeObject 這個試試 12/19 16:27
cancelpc: 改一次讀單列,或 stream 方式處理。 12/19 18:44
cancelpc: 剛看到 readline 也有問題! 12/19 18:45
cancelpc: 你單列處理完有釋放嗎?還是一直 add 那還不是都放在 me 12/19 18:49
cancelpc: mory 裡 12/19 18:49
感謝各位前輩的指點,Initialize()釋放後己經解決此問題 List<String> _sub = new List<string>(); string[] readtext = System.IO.File.ReadAllLines(failpath); foreach (string s in readtext) {//將檔案內容放置_sub陣列裡 _sub.Add(s); counter++; readtext.Initialize(); } ※ 編輯: takesi5200 (125.231.50.253), 12/19/2017 21:37:21