Reputation: 27
While i try to run TestRun, i got following errors. I tried a lot but i could not find it out. Help me! I got Out of memory error Java Heap. 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.
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:
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);
}
}
Upvotes: 1
Views: 13443
Reputation: 533880
Debugging this is very difficult for me.Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.
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.
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.
I missed increment operator inside switch case (n++). so the loop never ends.
I would get into the habit of using a for loop like
for(int i = 0; i <= 19; i++)
Upvotes: 2
Reputation: 3078
You are getting this error because of JVM is running out of memory.You need to increase java heap size by setting JVM parameters.
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.
Upvotes: 2
Reputation: 795
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.
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.
Upvotes: 0
Reputation: 7836
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.
To get rid of this you can increase these parameters in jvm:
The -Xms option sets the initial and minimum Java heap size. The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection.
This option sets the maximum Java heap size. The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection. Depending upon the kind of operating system you are running, the maximum value you can set for the Java heap can vary.
For more information visit http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html
Upvotes: 1
Reputation: 329
Change the java heap size in the jvm while running the application using -Xmx and -Xms options as below:
java -Xmx512M -Xms512M TestRun
Upvotes: 0