简体   繁体   English

java.lang.OutOfMemoryError(超出了GC开销限制)

[英]java.lang.OutOfMemoryError(GC overhead limit exceeded)

While i try to run TestRun, i got following errors. 当我尝试运行TestRun时,出现以下错误。 I tried a lot but i could not find it out. 我尝试了很多,但找不到。 Help me! 帮我! I got Out of memory error Java Heap. 我收到了内存不足错误Java堆。 Running the following code also takes more time to run, at last it shows error as given below. 运行以下代码也需要更多时间,最后显示错误,如下所示。 How to make this code right? 如何使此代码正确? Debugging this is very difficult for me.Exception in thread "main" java.lang.OutOfMemoryError: Java heap space. 对我来说,调试此过程非常困难。线程“ main”中的异常java.lang.OutOfMemoryError:Java堆空间。

package website;

import java.util.ArrayList;

import Combination.averageM;
import Combination.boatandstreamM;
import Combination.fractionsM;
import Combination.interestM;
import Combination.measurementM;
import Combination.numberM;
import Combination.percentageM;
import Combination.profitandlossM;
import Combination.simplificationM;
import Combination.timeanddistanceM;
import Combination.timeandwM;

public class ArrayOfSums {

    ArrayList<sums> als;
    String Q, A, scale, finalvalue;
    double fv;
    NewOptions pencil = new NewOptions();
    sums sumsObj;

    public ArrayList<sums> giveArrayOfSums(int x) {

        als = new ArrayList<sums>();
        int n = 0;
        switch (x) {

            case 1:
                simplificationM pen = new simplificationM();

                while (n <= 19) {

                    pen.simplificationM();
                    pencil.optionsCreator(pen.fv, pen.scale);
                    sumsObj = new sums(pen.Q, pen.A, pen.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 2:
                numberM pen1 = new numberM();

                while (n <= 19) {

                    pen1.numberM();
                    pencil.optionsCreator(pen1.fv, pen1.scale);
                    sumsObj = new sums(pen1.Q, pen1.A, pen1.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 3:
                percentageM pen2 = new percentageM();

                while (n <= 19) {

                    pen2.percentageM();
                    pencil.optionsCreator(pen2.fv, pen2.scale);
                    sumsObj = new sums(pen2.Q, pen2.A, pen2.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 4:
                numberM pen3 = new numberM();

                while (n <= 19) {

                    pen3.numberM();
                    pencil.optionsCreator(pen3.fv, pen3.scale);
                    sumsObj = new sums(pen3.Q, pen3.A, pen3.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 5:
                fractionsM pen4 = new fractionsM();

                while (n <= 19) {

                    pen4.fractionsM();
                    pencil.optionsCreator(pen4.fv, pen4.scale);
                    sumsObj = new sums(pen4.Q, pen4.A, pen4.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 6:
                profitandlossM pen5 = new profitandlossM();

                while (n <= 19) {

                    pen5.profitandlossM();
                    pencil.optionsCreator(pen5.fv, pen5.scale);
                    sumsObj = new sums(pen5.Q, pen5.A, pen5.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 7:
                averageM pen6 = new averageM();

                while (n <= 19) {

                    pen6.averageM();
                    pencil.optionsCreator(pen6.fv, pen6.scale);
                    sumsObj = new sums(pen6.Q, pen6.A, pen6.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 8:
                timeandwM pen7 = new timeandwM();

                while (n <= 19) {

                    pen7.timeandwM();
                    pencil.optionsCreator(pen7.fv, pen7.scale);
                    sumsObj = new sums(pen7.Q, pen7.A, pen7.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 9:
                timeanddistanceM pen8 = new timeanddistanceM();

                while (n <= 19) {

                    pen8.timeanddistanceM();
                    pencil.optionsCreator(pen8.fv, pen8.scale);
                    sumsObj = new sums(pen8.Q, pen8.A, pen8.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 10:
                boatandstreamM pen9 = new boatandstreamM();

                while (n <= 19) {

                    pen9.boatandstreamM();
                    pencil.optionsCreator(pen9.fv, pen9.scale);
                    sumsObj = new sums(pen9.Q, pen9.A, pen9.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3, pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 11:
                interestM pen11 = new interestM();

                while (n <= 19) {

                    pen11.interestM();
                    pencil.optionsCreator(pen11.fv, pen11.scale);
                    sumsObj = new sums(pen11.Q, pen11.A, pen11.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3,
                            pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

            case 12:
                measurementM pen12 = new measurementM();

                while (n <= 19) {

                    pen12.measurementM();
                    pencil.optionsCreator(pen12.fv, pen12.scale);
                    sumsObj = new sums(pen12.Q, pen12.A, pen12.finalvalue, pencil.jRadioButton1, pencil.jRadioButton2, pencil.jRadioButton3,
                            pencil.jRadioButton4);
                    als.add(sumsObj);
                }
                break;

        }

        return als;

    }

}

Class Sum: 类别总和:

package website;

public class sums {
    public String Question, Answer, Finalvalue, P1, P2, P3, P4;
    public int choosenOption;

    public sums(String Q, String A, String finalvalue, String p1, String p2, String p3, String p4) {
        Question = Q;
        Answer = A;
        Finalvalue = finalvalue;
        P1 = p1;
        P2 = p2;
        P3 = p3;
        P4 = p4;

    }

    public void setChoosen(int n) {
        choosenOption = n;
    }

}

Class TestRun: 类TestRun:

package website;

import java.util.ArrayList;

public class TestRun {

    /**
     * @param args
     */
    public static void main(final String[] args) {
        ArrayOfSums SumsTable = new ArrayOfSums();
        //session.setAttribute("sumtin",SumsTable.giveArrayOfSums(8));

        ArrayList<sums> SumsSet = (ArrayList<sums>) SumsTable.giveArrayOfSums(8);
        sums gotSum = (sums) SumsSet.get(1);

        System.out.println(gotSum.Question);

    }
}

Debugging this is very difficult for me.Exception in thread "main" java.lang.OutOfMemoryError: Java heap space. 对我来说,调试此过程非常困难。线程“ main”中的异常java.lang.OutOfMemoryError:Java堆空间。

It is not a problem you can debug. 您可以调试这不是问题。 You are using too much memory for the memory limit you have. 您使用过多的内存来满足您的内存限制。 There is no one line of code which might cause this problem. 没有一行代码可能会导致此问题。

Instead you need to use a profiler to determine where the memory has been used. 相反,您需要使用探查器来确定内存的使用位置。 I suggest you try increasing your maximum to just under half your main memory and attach a profiler (VisualVM is better than nothing) and see if where objects are being retained. 我建议您尝试将最大内存增加到接近主内存的一半,并附加一个探查器(VisualVM总比没有好),看看是否将对象保留在哪里。

If you use a commercial profiler this will run a little better and be clearer as to where the problem is. 如果使用商用分析器,则运行情况会更好一些,并且可以更清楚地了解问题所在。 I use YourKit and you can get an eval license for free. 我使用YourKit,您可以免费获得评估许可证。

I missed increment operator inside switch case (n++). 我错过了开关盒(n ++)内的增量运算符。 so the loop never ends. 因此循环永远不会结束。

I would get into the habit of using a for loop like 我会养成使用for循环这样的习惯

for(int i = 0; i <= 19; i++)

You are getting this error because of JVM is running out of memory.You need to increase java heap size by setting JVM parameters. 由于JVM内存不足,您将收到此错误。您需要通过设置JVM参数来增加Java堆大小。
I think problem may be here, 我认为问题可能在这里,

 while (n <= 19) {

                pen.  simplificationM ();
                pencil.optionsCreator(pen.fv, pen.scale);

                sumsObj = new sums(pen.Q, pen.A, pen.finalvalue 
                                  , pencil.jRadioButton1, pencil.jRadioButton2
                                  , pencil.jRadioButton3, pencil.jRadioButton4);
                als.add(sumsObj);
                n++;//added to increase value of n in each iteration
            }
            break;

In above code you are not increasing value of n .So while(n<=19) causing infinite loop. 在上面的代码中,您没有增加n值,所以while(n<=19)导致无限循环。

java.lang.OutOfMemoryError is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. 当Java虚拟机由于内存不足而无法分配对象,并且垃圾回收器无法再提供更多内存时,将引发java.lang.OutOfMemoryError

To get rid of this you can increase these parameters in jvm: 为了摆脱这种情况,您可以在jvm中增加以下参数:

  • Xms - min heap size Xms-最小堆大小

The -Xms option sets the initial and minimum Java heap size. -Xms选项设置Java堆的初始大小和最小大小。 The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection. Java堆(“堆”)是内存的一部分,在内存中,内存块分配给对象并在垃圾回收期间释放。

  • Xmx - max heap size Xmx-最大堆大小

This option sets the maximum Java heap size. 此选项设置最大Java堆大小。 The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection. Java堆(“堆”)是内存的一部分,在内存中,内存块分配给对象并在垃圾回收期间释放。 Depending upon the kind of operating system you are running, the maximum value you can set for the Java heap can vary. 根据您所运行的操作系统的类型,可以为Java堆设置的最大值可能有所不同。

For more information visit http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html 有关更多信息,请访问http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html

使用-Xmx和-Xms选项在运行应用程序时,更改jvm中的Java堆大小,如下所示:

java -Xmx512M -Xms512M TestRun 

This occurs when either the memory used in the heap is above the allowed or objects such as lists are getting so big they are exceeding the JVM's allocated memory. 当堆中使用的内存超出允许范围,或者列表之类的对象变得太大而超过了JVM分配的内存时,就会发生这种情况。
This can be solved by taking care of arrays such as to empty them when objects are not needed, flushing memory with the garbage collector, trying not to use arrays in loops and simply taking care of the stack. 这可以通过照顾数组来解决,例如在不需要对象时将其清空,用垃圾回收器刷新内存,尝试不在循环中使用数组以及仅照顾堆栈。 It is bad practice to simply increase the JVMs allowed memory unless it is absolutely necessary. 除非绝对必要,否则简单地增加JVM允许的内存是个坏习惯。

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

相关问题 Java PreparedStatement java.lang.OutOfMemoryError:超出了GC开销限制 - Java PreparedStatement java.lang.OutOfMemoryError: GC overhead limit exceeded 詹金斯 java.lang.OutOfMemoryError:超出 GC 开销限制 - Jenkins java.lang.OutOfMemoryError: GC overhead limit exceeded java.lang.OutOfMemoryError:GC开销限制超出了android studio - java.lang.OutOfMemoryError: GC overhead limit exceeded android studio Gridgain:java.lang.OutOfMemoryError:超出了GC开销限制 - Gridgain: java.lang.OutOfMemoryError: GC overhead limit exceeded Spark失败了java.lang.OutOfMemoryError:超出了GC开销限制? - Spark fails with java.lang.OutOfMemoryError: GC overhead limit exceeded? SonarQube java.lang.OutOfMemoryError:超出了GC开销限制 - SonarQube java.lang.OutOfMemoryError: GC overhead limit exceeded Tomcat java.lang.OutOfMemoryError:超出了GC开销限制 - Tomcat java.lang.OutOfMemoryError: GC overhead limit exceeded java.lang.OutOfMemoryError:超出 GC 开销限制 - java.lang.OutOfMemoryError: GC overhead limit exceeded 超出Junit java.lang.OutOfMemoryError GC开销限制 - Junit java.lang.OutOfMemoryError GC overhead limit exceeded 获取错误:java.lang.OutOfMemoryError:超出了GC开销限制 - Getting Error:java.lang.OutOfMemoryError: GC overhead limit exceeded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM