js-gc
All the languages I’ve had major exposure to (JS, Ruby, Java, Scala) implement their own garbage collection.
I was reading a bit about C
last night, where this luxury is not provided and memory management is part of the developer’s duties.
It turns out JavaScript (via the browsers that implement it) uses a mark-and-sweep approach to garbage collection. My understanding of this is, the Garbage Collector basically walks the entire dependency tree, visiting each “reachable” object (meaning an object that is referenced by another object). The GC dumps all the unreachable objects
Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management
A quick example, and reminder that JS objects are passed by reference:
a
and b
are both pointing at the same array object in memory. They are both references to that object.
Now the reference to the array has been lost and it can be safely erased from memory.