How long is the data allocated to functions?

Asked

Viewed 80 times

6

In one language, (I don’t know if there’s any difference in others, but you can consider Javascript) when I have this situation:

function a() {
    b();
}

function b() {
    c();
}

function c() {
    d();
}

function d() {
    e();
}

function e() {
    f();
}

function f() {

}

What happens to things that are being created within methods? As it goes running it will release the memory or it keeps up the method F execute?

1 answer

4


First nothing is running within these functions. There is no memory allocation, therefore there is nothing to be released.

If the example had some variable being allocated, at the end of the execution of each function, the content is released. Not the memory space occupied, since the memory is pre-allocated. Local function variables are placed in the pile (stack). Then each function call goes up the stack, and at the end goes back to the original position.

Then whatever is allocated in a, b, c, d, e will be kept there while these functions are running, they have only temporarily delegated to another function. As each one of them ends up releasing. The name stack is precisely to identify this well. You can’t release what’s underneath, to get something out of the pile that’s not on top just by taking out what’s above.

The stack exists whether to use it or not (it is impossible not to use it on anything that does anything at all useful). The space allocated to the stack is fixed, using or not. If it exceeds stack space occurs the famous stack overflow.

In general all languages work like this.

If there is any allocation on heap (see link above) this will not be released. The memory release strategy of the heap varies from language to language. Some require the programmer to do this, some do it more or less automatically, others do it perfectly, and still others do it to some extent. In general goes on the heap what is too big for the stack or that needs to survive at the end of the function.

  • Regarding having nothing within the methods, it is due to being just an example. Bigown, so, in case I wanted this pile not to be kept, I would have to basically not have a pile, right? For a case where I need one function to perform another, I could use the state machine approach to achieve the same result as this stack, thus avoiding the "problem" of one function having to wait for the result of another to be able to release what it needs, right?

  • I improved the answer. Read the links passed to better understand. The question and the example are very misguided, you can understand what you want but it does not represent well what is being asked, just confuses. State machine is something else and does not seem to have to do with this question, if it was the intention, need to ask another question. And whether or not to wait for the execution of the other depends on what you want. It makes sense, but not waiting for the other does not mean that it fails to perform. The battery will be used anyway. And if you do not know what you are doing can compromise it. Or it can impair performance.

  • @Paulogustavo In the specific case of JS the thing is a little more complicated. In your example there is not much secret, but if there is closures have things that can stay "alive" indefinitely (alive = not removed by garbage collector).

Browser other questions tagged

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