Angular Memory Leaks 1 - A Sawtooth

Demonstrates a sawtooth memory usage timeline.

by Dave Kerr

HTML

<h3>A Sawtooth</h3>
<p>Demonstrates a sawtooth pattern of memory usage. We continually allocate memory, but it is always shortlived. The garbage collector periodically cleans it up and we return to where we started.</p>

<button id="start">Start</button>

JavaScript

document.getElementById("start").onclick = function() {
    window.setInterval(function() {
        
        //    Create an array of numbers.
        var count = Math.random() * 10000;
        var numbers = [];
        for(var i=0; i<count; i++)
            numbers[i] = Math.random() * 10;
        var sum = 0;
        for(var i=0; i<count; i++)
            sum += numbers[i];
        console.log("Some random numbers add up to: " + sum);   
        
    }, 100);
};