-1

I think it's a little bit strange question. But I'll explain what I mean.

It turns out that Mark-and-sweep algorithm clears the object references in this example after going out from this function.

function f() {
    let a = 'some text';
    var obj1 = {};
    var obj2 = {};
    obj1.p = obj2; // obj1 references obj2
    obj2.p = obj1; // obj2 references obj1. This creates a cycle.
}
f();

So, if Mark-and-sweep can clear only objects, do all variables in this function stay? I mean that we can't use them out of the function. Is that behaviour just because of "JS rules"?

P. S. I mean that before knowing this information I thought that we can't use all variables from function because they are deleted after we go out from this function.

Ivan
  • 478
  • 2
  • 13
  • 1
    Why would they stick around if they are not being referenced? Maybe I am missing the point of the question? – epascarello Apr 14 '21 at 17:38
  • @epascarello yes, I checked this in devtools. It turns out that Mark-and-sweep works not only with objects (the variables are deleted after going out from this function). I've just read about this algorithm in relation with objects. There were examples only with objects. What's that supposed to mean? – Ivan Apr 14 '21 at 17:46
  • 1
    Hard to guess, but in this case those variables are all short lived since nothing uses them outside the scope of that function. – epascarello Apr 14 '21 at 17:50
  • 1
    The marking requires a set of root references to start from. The set of root references includes all global variables and the local variables of currently ongoing function evaluations. If the particular interpreter allows garbage collection to take place in the middle of an expression evaluation, temporary results on the stack are also root references. The sweeping can only clear objects, as local variables don’t need the garbage collector anyway. Their cleanup is implied when the function ends. But the objects formerly referenced by them require GC to prove that there are no other references. – Holger Apr 15 '21 at 12:53

1 Answers1

1

It turns out that Mark-and-sweep algorithm clears the object references in this example after going out from this function

It could be mark-and-sweep. It could be reference counting. They could be on the stack (yes, objects can be on the stack). It could be any of several other garbage collection strategies. At the end of the day, all that matters in JavaScript terms is that neither of those objects is reachable anymore once that function returns, and so the JavaScript engine can — using any heuristic or algorithm — remove those objects from memory. (It can also not do so, garbage collection isn't mandatory, but it's universal in the real world outside of specialized environments, such as some embedded ones.)

So...do all variables in this function stay? I mean that we can't use them out of the function. Is that behaviour just because of "JS rules"?

None of them stay, most likely, since nothing uses any of them.

P. S. I mean that before knowing this information I thought that we can't use all variables from function because they are deleted after we go out from this function.

That depends on whether any closures are created within the function that are referenced once the function returns. In your example, none are, so the entire context of the call to f can be removed from memory. But that's not true here:

function counter(value) {
    return function() {
        return value++;
    };
}

const f = counter(0);
console.log(f()); // 0
console.log(f()); // 1
console.log(f()); // 2

There, the function that is returned closes over the environment of the call to counter, so the value parameter (which is essentially the same as a local variable) is not cleaned up, it's retained because the environment is retained because the function has a reference to it. In that code, as long as the f constant has a reference to the function, the environment and the value within it are retained.

More:

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Then why does [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#mark-and-sweep_algorithm) say that Mark-and-sweep algorithm impact on the cleaning of circular references? – Ivan Apr 14 '21 at 19:47
  • In fact, in this example, all circular references are cleared, as I understand, due to the way of working local variables (they are not visible outside of the function and also they are almost always available only while the function is running). – Ivan Apr 14 '21 at 19:47
  • And since the reference to object is passed to the variable, and that variable is available only during the working of function, it's still not clear to me what MDN wanted to say here. – Ivan Apr 14 '21 at 19:48
  • I just think... What's that got to do with Mark-and-sweep algorithm? – Ivan Apr 14 '21 at 19:50
  • @Ivan - It doesn't. That MDN page also talks about other allocation strategies. The page claims (without citation -- remember that MDN is very good, but it *is* community-edited) that "As of 2012, all modern browsers ship a mark-and-sweep garbage-collector." Well, that may have been true in 2012, and it may be true now, but it's not required by the specification. A JavaScript implementation can use other strategies if the authors want to. (Note that V8 has gone through **massive** changes since 2012, including getting a completely new interpreter and compiler. Nine years is a long time in JS.) – T.J. Crowder Apr 15 '21 at 06:57