How to avoid memory overflow with recursion

-3

I have a function that is called recursively, how to avoid memory overflow ??

int cont = 0;

public void recur() {
    recur();

    cont ++;
    System.out.println("Chamado: " + cont);
}
    
asked by anonymous 14.10.2017 / 02:24

2 answers

2

You will need to add an exit clause. For example checking if count == 10,000.

Another option that can happen naturally is that the memory of the machine will end, or if it has a lot of memory, the count value may be greater than the maximum Integer value. [Integer.MAX_VALUE]

    
17.10.2017 / 21:38
3

You need an exit criterion for recursion.

    
14.10.2017 / 02:26