2017-10-08 82 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每次有海峡的新值。所以你有一个指数的内存使用。

相关问题