java.lang.OutOfMemoryError:Java堆空间哪些对象获取所有内存?

时间:2017-10-08 22:30:24

标签: java xslt

我写了一些似乎在内存方面爆炸的代码。

我不明白为什么,因为大多数对象是用补充方法创建的,我希望在方法结束后空间可以自由。?!?(或不?)

我对记忆消费主题很新。我不知道如何改进它。

通过添加标志-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(第二段代码)之后

由于

1 个答案:

答案 0 :(得分:3)

看起来问题就在于:

"

每次有str的新值时,此代码段将使列表LOP的大小加倍。所以你有一个指数内存使用。