標籤:close dump tar file image rename doc name write
註:本文主要記錄這次解決記憶體溢出問題的過程而不是具體問題。
最近在寫一個搜尋引擎,使用倒排索引結構進行文檔檢索,儲存索引的基本思想是先將倒排列表儲存到記憶體中一個有序Map裡(TreeMap),然後當記憶體佔用達到一定閾值的時候將記憶體中的倒排列表有序寫入磁碟,當磁碟已經存在索引時,則將記憶體中的索引和磁碟中的索引進行合并,產生新的索引,合并過程類似于歸並排序。合并記憶體索引和磁碟索引的代碼如下:
public synchronized void merge(){ LogUtil.info("InvertIndex merge start..."); File f=new File(path); //這個檔案是原磁碟上的索引檔案
//磁碟存在索引,合并磁碟索引和記憶體索引 if (f.exists()) { String outPath=path+".temp"; File outFile=new File(outPath); TreeMap<String, TreeSet<Long>> ramSnapshot=null; ramSnapshot=ram; //ram儲存的是記憶體索引,這裡因為ram可能被其他添加文檔的線程修改,因此先存一份快照然後將ram清空,後面實際操作的是快照 ram=new TreeMap<>(); BufferedReader reader=null; PrintWriter writer=null; try {
//合并過程,有3個指標:分別指向記憶體索引的當前位置、原磁碟索引讀到的位置、新的磁碟索引寫入的位置 Iterator<Entry<String, TreeSet<Long>>> ramIterator= ramSnapshot.entrySet().iterator(); reader=new BufferedReader(new FileReader(f)); writer=new PrintWriter(new BufferedWriter(new FileWriter(outFile))); Entry<String, TreeSet<Long>> entry=ramIterator.hasNext()?ramIterator.next():null; String line=reader.readLine(); while (entry!=null&&line!=null) { long freeRam=Runtime.getRuntime().freeMemory()/1000/1000; System.out.println("freeRam: "+freeRam); String ramWord=entry.getKey(); String diskWord=line.split(separator1)[0]; String out=""; int c=ramWord.compareTo(diskWord);
//合并過程,因為是兩個有序列表,採用類似歸併排序的合并方法,區別在於這裡如果遇到倒排詞相等的時候,需要合并到一個倒排詞(合并兩者文檔列表) if (c==0) { TreeSet<Long> ramDocIds=entry.getValue(); TreeSet<Long> diskDocIds=this.convertLine2DocIds(line); TreeSet<Long> union=ramDocIds; union.addAll(diskDocIds); out=this.convertIndex2Line(ramWord, union); entry=ramIterator.hasNext()?ramIterator.next():null; line=reader.readLine(); }else if (c<0) { out=this.convertIndex2Line(ramWord, entry.getValue()); entry=ramIterator.hasNext()?ramIterator.next():null; }else { out=this.convertIndex2Line(diskWord, this.convertLine2DocIds(line)); line=reader.readLine(); } writer.println(out); } LogUtil.info("InvertIndex complex merge complete."); while (ramIterator.hasNext()) { entry=ramIterator.next(); String out=this.convertIndex2Line(entry.getKey(), entry.getValue()); writer.println(out); } LogUtil.info("InvertIndex ram merge complete."); while ((line=reader.readLine())!=null) { writer.println(line); } LogUtil.info("InvertIndex disk merge complete."); } catch (Exception e) { LogUtil.err("merge ram index and disk index fail.", e); }finally { try { if (reader!=null) { reader.close(); } if (writer!=null) { writer.close(); } } catch (Exception e2) {LogUtil.err("release resource fail.", e2);} } f.delete(); if (!outFile.renameTo(new File(path))) { throw new RuntimeException("rename temp file fail."); } }else { //磁碟上原本不存在索引,直接將記憶體索引寫入磁碟
//代碼略
}
代碼的主要思想是維持3個指標:記憶體索引是一個有序TreeMap,iterator相當於一個虛擬指標;磁碟索引也是有序的,BufferdReader相當於一個虛擬指標;合并後產生的新索引使用BufferdWriter作為指標。然後使用歸併排序的思想,比較記憶體索引詞和磁碟索引詞的大小,哪個小就將哪個寫入新的索引然後將其指標前進一位,如果兩個索引詞相等,則合并兩者的文檔列表。
從上面的描述來看以上代碼使用的記憶體應該是O(1)的,因為記憶體中除了記憶體索引之外,同一時刻只會從磁碟索引讀出一行,但是實際啟動並執行時候,總是在合并時報出GC overhead limit exceeded,這個異常就是說jvm用了大量時間(超過98%)執行GC但是只釋放了很少的堆記憶體(小於2%),換句話說就是OOM的前兆。根據我對程式的記憶體佔用的分析,這種情況是不正常的。
於是給程式添加 “-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:/dump” 參數,在程式出現記憶體溢出異常的時候dump出記憶體資訊。
用eclipse MAT查看佔用記憶體最多的對象:
發現是TreeMap的執行個體,在這個程式中我用了TreeMap來儲存記憶體索引,而在合并索引的過程中,記憶體索引的大小應該是不變的,那麼為什麼會溢出呢?
通過看代碼將問題定位在了紅色代碼處:
這裡首先將ramDocIds指向entry.getValue(),然後又將union指向ramDocIds,此時union實際上是直接指向entry.getValue()的,然後union執行了addAll操作。。眾所周知,addAll操作是將元素加到對象本身的,這裡我的原意是聲明一個局部變數儲存列表合并結果然後存入新的索引檔案,但是無意中卻同時修改了記憶體索引導致記憶體索引越來越大。
OK,到這裡問題就解決了,只需要將
TreeSet<Long> union=ramDocIds;
改為
TreeSet<Long> union=new TreeSet<>(ramDocIds);
即可。
回顧一下這次解決記憶體溢出問題的過程:
- 剖析器空間複雜度,看記憶體溢出的是否正常。
- 添加jvm參數,讓程式在記憶體溢出的時候dump出記憶體快照。
- 使用eclipse MAT分析佔用記憶體最多的對象。
- 在源碼中找到相應的對象尋找問題。
記一次java記憶體溢出的解決過程