How to avoid memory bursting with recursiveness

Asked

Viewed 286 times

-3

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

int cont = 0;

public void recur() {
    recur();

    cont ++;
    System.out.println("Chamado: " + cont);
}
  • Related: https://answall.com/q/237151/64969

  • What are you trying to do with that?

  • Basically I take all the links from one page, and then I call the function to pick up all the links from each page of each link, and so recursively.

  • @Lucascarezia you can do this with navigation on graphs, but this is subject for another question

2 answers

3

You need an output criterion for recursion.

  • Yes. Just that. Putting that there will be no pile burst

  • 1

    Indeed, it would be appropriate to also have a proof/proof/proof/conjecture that the base case of recursion is reached

  • There’s a way out, but basically it’s almost infinite.

2


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

Another option that can happen naturally is that the machine will run out of memory, or if it has TOO much memory, the value of Count may be greater than the maximum value of Integer. [Integer.MAX_VALUE]

  • Thus, if each recursive function occupies a single byte of memory (occupies much more, in fact), you would use 2GB only to reach that level of recursive call depth. I think to assume this value of the whole maximum was to stretch and very the ballast...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.