简体   繁体   English

代码中超出了 GC 开销限制

[英]GC overhead limit exceeded in code

I have written a code.我写了一个代码。 The problem that I am facing is that when the "j" of for loop exceeds 1000 I start to get an error of "GC overhead limit exceeded".我面临的问题是,当 for 循环的“j”超过 1000 时,我开始收到“超出 GC 开销限制”的错误。 If I increase the allocated memory to 4GB I can iterate upto 2000 after which the same problem occurs.如果我将分配的内存增加到 4GB,我可以迭代到 2000 次,之后会发生同样的问题。 I want to keep this option of increasing the memory as the last resort and want to try to scale my code.我想保留这个增加内存的选项作为最后的手段,并想尝试扩展我的代码。 The compiler highlights a problem with the statements where I have placed an arrow.编译器突出显示了我放置箭头的语句的问题。 Can someone please guide me that what could be the possible error here.有人可以指导我这里可能出现的错误是什么。 I have already visited this question Error java.lang.OutOfMemoryError: GC overhead limit exceeded我已经访问过这个问题错误 java.lang.OutOfMemoryError: GC 开销限制超出

     for (int j=1; j<=num_doc; j++) { 
        List<Integer> list1 = new ArrayList<Integer>(Collections.nCopies(129039, 0));
        BufferedReader fl = new BufferedReader(new FileReader(dataFolder+"file"+ " ("+j+")"+".int"));
        String line1;

        while((line1=fl.readLine()) != null) {

            String[] arr=line1.split(" ");//<---------------------
            line1="";
            int k = Integer.parseInt(arr[0]);
            Arrays.fill(arr, "");
            numb=numb+1;
            int temp=(list1.get(k))+1;
            list1.set(k, temp);
        }
        F_d.add(numb);
        numb=0;
        fl.close();
        ls2d.add(new ArrayList<Integer>(list1));//<---------------------
        list1.clear();
    }

Two things can be immediately optimized for less memory requirements:可以立即优化两件事以减少内存需求:

        // we don't need all the fragments, taking only the first is fine
        String firstElem=line1.substring(0, line1.indexOf(" "));
        line1=null;// let GC collect this at its convenience
        int k = Integer.parseInt(firstElem);

then然后

    // Don't add the copy, add list1 itself
    // You are initing list1 in the beginning of the for cycle anyway
    // and until then nothing happens.
    ls2d.add(list1);//<---------------------
    // list1.clear(); -- since we added list1, we don't clear it anymore

Here's a couple of ideas to reduce memory consumption - and runtime probably.这里有一些减少内存消耗的想法 - 可能还有运行时间。

  • use an array instead of an ArrayList - you don't seem to use ArrayList specific functionality so an array will be more compact and easier to work with使用数组而不是 ArrayList - 您似乎没有使用 ArrayList 特定功能,因此数组将更紧凑且更易于使用
  • tell split to just read the first field of a line告诉 split 只读取一行的第一个字段

Note that I removed all code that was intended to coerce the garbage collector into cleaning up, I don't think it helps in this case.请注意,我删除了所有旨在强制垃圾收集器进行清理的代码,我认为在这种情况下没有帮助。


 for (int j=1; j<=num_doc; j++) { 
    int[] list1 = new int[129039];

    BufferedReader fl = new BufferedReader(new FileReader(dataFolder+"file"+ " ("+j+")"+".int"));
    String line1;

    while((line1=fl.readLine()) != null) {
        String[] arr=line1.split(" ",2); // Just read first field - see String Javadoc
        int k = Integer.parseInt(arr[0]);
        list[k]=list[k]+1;
        numb=numb+1;
    }
    F_d.add(numb);
    numb=0;
    fl.close();
    ls2d.add(list1);// you'll obviously need to change ls2d's type, or reconvert using Arrays.asList
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM