Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
3 contributors

Users who have contributed to this file

@dr4b @Meggin @jakub-g
167 lines (132 sloc) 3.23 KB
{{+bindTo:partials.standard_devtools_article}}
<!DOCTYPE html>
<h1>Example 8</h1>
<h1>Example 8: Recording heap allocations</h1>
<p>Let's analyze the impact of closures on memory</p>
<p>You can see that 'eval' retains a reference on all the variables of the closure.</p>
<ol>
<li>Start to record heap allocations</li>
<li>Start the creation of new closures</li>
<li>Analyze what is happening</li>
</ol>
<p><button onclick="largeClosures()">Closures mantain hold on large string</button></p>
<p><button onclick="smallClosures()">Closures mantain hold only on small
strings</button></p>
<p><button onclick="evalClosures()">Closures with eval</button></p>
<p><button onclick="stopAndClear()">Stop and clear</button></p>
<script>
'use strict';
var intervalId,
closures = [];
function createLargeClosure() {
var largeStr = new Array(1000000).join('x');
return function lC() {
return largeStr;
};
}
function createSmallClosure() {
var smallStr = 'x';
var largeStr = new Array(1000000).join('x');
return function sC() {
return smallStr;
};
}
function createEvalClosure() {
var smallStr = 'x';
var largeStr = new Array(1000000).join('x');
return function eC() {
eval('');
return smallStr;
};
}
function largeClosures() {
stopInterval();
intervalId = setInterval(function() {
closures.push(createLargeClosure());
}, 1000);
}
function smallClosures() {
stopInterval();
intervalId = setInterval(function() {
closures.push(createSmallClosure());
}, 1000);
}
function evalClosures() {
stopInterval();
intervalId = setInterval(function() {
closures.push(createEvalClosure());
}, 1000);
}
function stopInterval() {
if (intervalId) {
clearInterval(intervalId);
}
intervalId = null;
}
function clear() {
closures.length = 0;
}
function stopAndClear() {
stopInterval();
clear();
}
</script>
<hr>
<pre>
'use strict';
var intervalId,
closures = [];
function createLargeClosure() {
var largeStr = new Array(1000000).join('x');
return function lC() {
return largeStr;
};
}
function createSmallClosure() {
var smallStr = 'x';
var largeStr = new Array(1000000).join('x');
return function sC() {
return smallStr;
};
}
function createEvalClosure() {
var smallStr = 'x';
var largeStr = new Array(1000000).join('x');
return function eC() {
eval('');
return smallStr;
};
}
function largeClosures() {
stopInterval();
intervalId = setInterval(function () {
closures.push(createLargeClosure());
}, 1000);
}
function smallClosures() {
stopInterval();
intervalId = setInterval(function () {
closures.push(createSmallClosure());
}, 1000);
}
function evalClosures() {
stopInterval();
intervalId = setInterval(function () {
closures.push(createEvalClosure());
}, 1000);
}
function stopInterval() {
if (intervalId) {
clearInterval(intervalId);
}
intervalId = null;
}
function clear() {
closures.length = 0;
}
function stopAndClear(){
stopInterval();
clear();
}
</pre>
{{/partials.standard_devtools_article}}
You can’t perform that action at this time.