2017-10-08 83 views
2

我寫了一些代碼,似乎在內存方面爆炸。java.lang.OutOfMemoryError:Java堆空間哪些對象正在獲取所有內存?

我不明白爲什麼,因爲大部分對象在補充方法的創建和我預料的空間給method.?!?(or結束後成爲自由不是?)

我相當新的內存消費主題。我不知道該怎麼做來改善它。

通過添加標誌-Xmx8192來配置JVM並沒有幫助。它只讓我處理3個包。 (最初用-Xmx標誌處理了27個包,我達到了30)

我可以延遲它以某種方式給GC時間來釋放空間嗎? 或者這不會幫助嗎?

這是迄今爲止我所編寫的代碼:

public class mainToTest{ 

     public static void main(String [] args)throws IOException{  String str;  
      String home = "C:/Users/Eirini/Desktop/OP/";  
      String s = "13092017-1800";  
      File Descriptor;   
      final Charset ENCODING = StandardCharsets.UTF_8;  
      Path path = Paths.get(home+"output.xml");   
      List <String> LOP= new ArrayList(); 
      LOP.clear();  

      List<String> lines; 
      int i,j; 

      File [] a =(new File(home+s)).listFiles();   
      System.out.println("Packages found...");  
      for (i=0; i<a.length; i++){   
      System.out.print("For package " + i);    
      Descriptor=findDescriptor(a[i]);    
      XSL.transformation(Descriptor,new File 
      (home+"extractprocedureid.xslt")); 

      lines = Files.readAllLines(path, ENCODING);    
      str=lines.get(1);   
      if (LOP.isEmpty()){ 
      LOP.add(str);}   
      for(j=0; j<LOP.size(); j++){ 
      if(!(str.equals(LOP.get(j)))){ 
        LOP.add(str);}   
      } 
      }   
      System.out.println("");  
      System.out.println("Finished Procedures found:");   
      for (i=0; i<LOP.size();i++){   
      System.out.println(LOP.get(i)); } 


    } 


     public static File findDescriptor(File pckg){  
      String s;   
      int i,k;   
      int j=0; 
      File[] ind=pckg.listFiles();   
      System.out.println(" all Items and descriptor listed"); 

      k=ind.length; 

      for (i=0;i<k;i++){   
       System.out.println("File " +i);   
       if (ind[i].getName().endsWith("_immc.xml")){ 
        j=i; 
        i=200; 
        System.out.println("Descriptor found !!!!");     }    
       else{ 
       System.out.println(" not a descriptor. Moving to the next");}  }  

      return ind[j];    

     } 
} 

和XSL.transformation看起來像

public static void transformation (File immc,File xslt){ 

     Source xmlInput = new StreamSource(immc); 
     Source xsl = new StreamSource(xslt); 
     Result xmlOutput = new StreamResult(new File("C:/Users/Eirini/Desktop/OP/output.xml")); 

     try { 
       Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl); 
      transformer.transform(xmlInput, xmlOutput); 
     } 
     catch (TransformerException e) { 
       System.out.println("Exception caught"); 
     } 
     System.out.println("XSLT transformation finnished...The result can be found in file C:/Users/Eirini/Desktop/OP/output.xml"); 

    } 

錯誤通常是XSL.transformation(第二張之後發生代碼)

感謝

+0

只是簡介它。順便說一下'-Xmx8192'只給出8kb的內存,而不是'-Xmx8192m'。 (但我認爲這只是一個錯字。) – lexicore

+0

這些轉換文件的輸出有多大?對於您轉換的每個文件,您正在讀取文本行並附加到「LOP」ArrayList。所以,你將在內存中保存所有這些轉換文件的輸出。不要追加到你的'LOP'來查看事情是否保持不變,或者在每個文件後面調用'LOP.clear()'來查看是否停止拋出OOM錯誤,然後從那裏調整。 –

+0

是的我的確切命令是java -Xmx2048m -Xms512m mainToTest –

回答

3

貌似問題是林ES:

for(j=0; j<LOP.size(); j++){ 
     if(!(str.equals(LOP.get(j)))){ 
      LOP.add(str);}   
     } 

這個片段將增加一倍列表的大小LOP每次有海峽的新值。所以你有一個指數的內存使用。

相關問題