2017-09-11 35 views
1

我每次試圖加載和處理一堆.owl文件的時間面臨的問題java.lang.OutOfMemoryError在我的應用程序。我在一個目錄中有500個奇怪的.owl文件,我在for循環中使用OWL API的loadOntologyFromOntologyDocument()方法將它們逐個加載到內存中。但是,在方法加載一些本體之後,內存開始耗盡。未使用的對象引用沒有被垃圾收集器清理。我搜索了這個問題,並且使用了-Xmx來增加許多人建議的堆大小到5GB。但問題仍然存在。我感謝在這方面的任何幫助。Java垃圾收集未清理內存在加載本體文件

OWLOntologyManager owlManager = OWLManager.createOWLOntologyManager(); 
File folder = new File("G:\\OWL and OBO"); 
File[] listOfFiles = folder.listFiles(); 
for (int i = 0; i < listOfFiles.length; i++) { 
    if (listOfFiles[i].isFile()) { 
     System.out.println("File " + listOfFiles[i].getName()); 
     try{ 
      File sourceFile = new File("G:\\OWL and OBO\\" + listOfFiles[i].getName()); 
      OWLOntology ontology = owlManager.loadOntologyFromOntologyDocument(sourceFile); 
     } catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 
+0

5gb不是jvm真的會接受的。嘗試使用1024,2048和更多。我的經驗是,4k不應該克服工作 –

+0

5GB或任何其他號碼將工作,它不一定是一個標準的2^n號碼。但是,您的代碼(或「OWLOntologyManager」)實際上可能正在使用該內存。什麼是文件的大小和你有多少文件? – Zilvinas

回答

0

OWLOntologyManager似乎可能掛在文件之間的某些內存上。你有沒有嘗試在listOfFiles循環內移動第一行?爲每個文件創建一個新實例將會「更加昂貴」,但這比'破壞'要好。

File folder = new File("G:\\OWL and OBO"); 
File[] listOfFiles = folder.listFiles(); 
for (int i = 0; i < listOfFiles.length; i++) { 
    if (listOfFiles[i].isFile()) { 
     OWLOntologyManager owlManager = OWLManager.createOWLOntologyManager(); 
     System.out.println("File " + listOfFiles[i].getName()); 
     try{ 
      File sourceFile = new File("G:\\OWL and OBO\\" + listOfFiles[i].getName()); 
      OWLOntology ontology = owlManager.loadOntologyFromOntologyDocument(sourceFile); 
     } catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 
+0

它沒有中斷地工作。非常感謝。 – user8594397

+0

@ user8594397 Javadoc說:「OWLOntologyManager還管理本體和本體文檔之間的映射。爲了管理這樣的映射,它必須保存所有加載的數據。 – maaartinus