Question

I have a program which can be defined as something like this

reset() {
   //sets all variables to initial values
   //clears all arrays
   method1();
}

method1 (){
    //doSomeStuff;
    method2();
}

method2(){
    //doStuff
    method3();
}

method3(){
    //doStuff
    if (jobDone) reset(); //here the cycle closes
    else method2();
}

All these methods are quite calculations heavy. Depending on the input data and the result the program may do just a couple of cycles and throw a 'stack overflow' error.

I have changed the VM flag -Xss (-Xss8M) but this doesn't really solve the problem.

Is there any way to make it working almost infinitely?

Was it helpful?

Solution

Solution previously mentioned by Luiggi Mendoza: How to avoid stack overflow error

When you call reset, it calls method1, it calls method2, it calls method3 and it calls either reset or method2 both causing infinite cycle in recursion.

You probably want:

if (jobDone) return; // here the cycle realy closes

instead of

if (jobDone) reset(); //here the do _not_ close

In case you realy want infinite cycling of your code this will not cause SO due to method calling of reset or methodi:

// assuming jobDone is actually a method, you might need this variable
boolean startReset = true;
while (true) {
        if (startReset) {
            //sets all variables to initial values
            //clears all arrays

            //doSomeStuff from method1;
        }
        //doStuff from method2

        //doStuff
        startReset = jobDone;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top