Commits

Jonathan Fine committed 22dc6c6

counters-discuss.rst: Removed material belonging to counters-memory.rst.

Comments (0)

Files changed (1)

source/counters-discuss.rst

 when writing code to put in the semicolons yourself, rather then let
 JavaScript put them in.)
 
-
-Memory
-------
-
-Understanding garbage collection and memory leaks is an advanced
-topic, but even beginners should know that there can be problems here.
-Correct use of a suitable framework is the simplest way to avoid
-memory leaks.
-
-The code in the above example indicates how to construct and use such
-a framework.
-
-Garbage collection
-------------------
-The JavaScript garbage collection deletes objects provide it can
-discover that they can never be used again.  The simplest case is:
-
-   .. code-block:: javascript
-
-      x = [1, 2, 3, 4, 5, 6, 7];
-      x = null;
-
-The second assignment to *x* ensures that the original array (created
-by the array literal) can no longer be accessed, and so it can be
-garbage collected.
-
-After the namespace anonyomous function has executed there are no
-global objects holding even indirect references to the function.
-Therefore is can and will be garbage collected. However, as we shall
-see, the execution context of the function continues.
-
-IE memory leaks
----------------
-
-Internet Explorer, prior to IE8 (check) had **two memory heaps**, one
-for JavaScript and the other for the DOM.  Each heap had its own
-garbage collector.  
-
-This means that if a DOM node *d* held a reference to a JavaScript
-object *j* and also that *j* held a reference to *d* then neither
-garbage could collect *j* or *d*.  What's worse, even when the page
-was unloaded IE did not reclaim this memory.
-
-Thus, prior to IE8, JavaScript could cause Internet Explorer to leak
-memory.  This lost memory could be reclaimed only by closing the
-browser!
-
-Residue
--------
-
-The *example* DOM node continues to exist, as it can be reached from
-the *document* node.  It has an *onclick* attribute, which is the
-function created by the *onclick_factory*, with *models* as the
-argument.  So that function is not garbage collected.
-
-The function *example.onclick* retains a reference to *models* just as
-surely as executing
-
-   .. code-block:: javascript
-
-      var f = function(arg){ return [1, 2, 3, arg] };
-      var y = f(x);
-
-causes *y* to retain a reference to the value of *x*.
-
-Finally, *models* contains references to the *Counter* instances and
-thus, by the hidden prototype reference, to the *counter* prototype
-object.
-
-The *counter* prototype object lies in the execution context of the
-anonymous function, and as it happend that keeps alive the whole of
-the execution context.
-
-Reclaiming memory
------------------
-
-Notice that the JavaScript holds only two reference to DOM nodes,
-namely *document* and *example*.  However, the line of code
-
-   .. code-block:: javascript
-
-      example = undefined;    // Release reference to DOM node.
-
-means that the JavaScript does not hold a reference to *example*.
-
-The DOM node *example* holds a reference, via its *onclick* function,
-to the JavaScript on the page, and in particular to the *Counter*
-instances.  This cannot be avoided, because we want certain DOM nodes
-to change the state of the counters.
-
-However, the converse is not true.  We can write the JavaScript code
-so that it has but one reference to a DOM object, namely the
-*document* node held for example as a property of the *global* object.