java.lang.StackOverflow when using recursion

2

I'm doing a function that prints the percentage of the process, which ends when the counter reaches the target amount, but always throws me the following error:

Exception in thread "main" java.lang.StackOverflowError at pruebas.Bucles.recursion(Bucles.java:88)

The error is printed many times, here I leave my class:

public class Bucles {

    private static final long META = 1000000000;
    private long porcentaje;

    public void probar(){
         porcentaje = META / 100;
        recursion(0);
    }



    private void recursion(long i) {
       // System.out.println(i);
        if (i < META) {
            if (i > porcentaje && i % porcentaje == 0) {
                System.out.println("proceso recursión : " + i / porcentaje + " %");
            }

            recursion(i++);// <----- Está es la linea 88
        }
    }
}

I made the change as they indicate me to change the postincremento:

private void recursion(long i) {
    // System.out.println(i);
    if (i < META) {
        if (i > porcentaje && i % porcentaje == 0) {
            System.out.println("proceso recursión : " + i / porcentaje + " %");
        }

        recursion(++i);// <----- Está es la linea 88
    }
}

If I print the value of i (I decompose the line) something very strange happens:

    
asked by gibran alexis moreno zuñiga 02.06.2017 в 00:05
source

2 answers

2

The main problem is what is discussed in the answer of mmartinez7 (use ++i no i++ ). Now, what happens is that you have many calls stored in the call stack, so the same error occurs: StackOverflowError . This happens for the number of times you are calling the recursion method, which is limited by the value of META .

Solutions:

  • Reduce the size of% co_of% to 100, for example. (The most effective)
  • If you want to keep the current size of META , you will need to increase the stack size using the META parameter. Example -Xss to say that the stack has a size of 100 MBs. (Unwanted, this is an index that your application has something strange).
  • answered by 02.06.2017 / 18:17
    source
    2

    The problem is that you are using the postincremento in your recursive call. The postincrement involves increasing the value by 1 for the next instruction.

    Your program could be written like this and it would be equivalent:

        private void recursion(long i) {
            if (i < META) {
                if (i > porcentaje && i % porcentaje == 0) {
                    System.out.println("proceso recursión : " + i / porcentaje + " %");
                }
    
                recursion(i);
                i++
            }
    
    }
    

    Basically your i is always 0 and will only be increased when you leave the recursive call, which will never happen since 0 < META

    The correct thing for what you try to do would be to use the pre-increment:

        private void recursion(long i) {
            if (i < META) {
                if (i > porcentaje && i % porcentaje == 0) {
                    System.out.println("proceso recursión : " + i / porcentaje + " %");
                }
    
                recursion(++i);
            }
    
    }
    
        
    answered by 02.06.2017 в 00:21