Beruflich Dokumente
Kultur Dokumente
Allocation in JavaScript
Value initialization
var o = {
a: 1,
b: null
}; // allocates memory for an object and contained values
function f(a) {
return a + 2;
} // allocates a function (which is a callable object)
var s = 'azerty';
var s2 = s.substr(0, 3); // s2 is a new string
// Since strings are immutable value,
// JavaScript may decide to not allocate memory,
// but just store the [0, 3] range.
Using values
Using values basically means reading and writing in allocated memory.
This can be done by reading or writing the value of a variable or an object
property or even passing an argument to a function.
References
The main notion garbage collection algorithms rely on is the notion
of reference. Within the context of memory management, an object is
said to reference another object if the former has an access to the latter
(either implicitly or explicitly). For instance, a JavaScript object has a
reference to its prototype (implicit reference) and to its properties values
(explicit reference).
In this context, the notion of "object" is extended to something broader
than regular JavaScript objects and also contains function scopes (or the
global lexical scope).
Example
var o = {
a: {
b: 2
}
};
// 2 objects are created. One is referenced by the other as
one of its properties.
// The other is referenced by virtue of being assigned to the
'o' variable.
// Obviously, none can be garbage-collected
Limitation: cycles
function f() {
var o = {};
var o2 = {};
o.a = o2; // o references o2
o2.a = o; // o2 references o
return 'azerty';
}
f();
Real-life example
var div;
window.onload = function() {
div = document.getElementById('myDivElement');
div.circularReference = div;
div.lotsOfData = new Array(10000).join('*');
};
In the above example, the DOM element "myDivElement" has a circular
reference to itself in the "circularReference" property. If the property is
not explicitly removed or nulled, a reference-counting garbage collector
will always have at least one reference intact and will keep the DOM
element in memory even if it was removed from the DOM tree. If the
DOM element holds lots of data (illustrated in the above example with the
"lotsOfData" property), the memory consumed by this data will never be
released.
Mark-and-sweep algorithm
This algorithm reduces the definition of "an object is not needed
anymore" to "an object is unreachable".
This algorithm assumes the knowledge of a set of objects called roots (In
JavaScript, the root is the global object). Periodically, the garbage-collector
will start from these roots, find all objects that are referenced from these
roots, then all objects referenced from these, etc. Starting from the roots,
the garbage collector will thus find all reachable objects and collect all
non-reachable objects.
This algorithm is better than the previous one since "an object has zero
reference" leads to this object being unreachable. The opposite is not
true as we have seen with cycles.
In the first above example, after the function call returns, the 2 objects
are not referenced anymore by something reachable from the global
object. Consequently, they will be found unreachable by the garbage
collector.