Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Testing out whether V8 within Chrome runs garbage collection on global variables (assigned to window)

View gist:4035423
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<script>
function logMemory() { console.log("Heap use(MB): " + window.performance.memory.usedJSHeapSize / 1000000, ", Total heap size(MB): " + window.performance.memory.totalJSHeapSize / 1000000) };
window.things = {};
things.array = [];
function leak() {
for(var i = 0; i < 5000000; i++) {
things.array.push("test string");
}
}
 
logMemory();
leak();
leak();
leak();
leak();
leak();
logMemory();
window.things = null;
 
setTimeout(function() {
logMemory();
 
}, 2500);
 
</script>
Owner

Make sure to run with the --enable-memory-info flag, if you want access to window.performance.memory

OS X

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --enable-memory-info

Owner

This article suggests that

global variables are not cleaned up by the garbage collector during the life of your page. Regardless of how long the page is open, variables scoped to the JavaScript runtime global object will stick around.

Experimentation in Chrome 23 suggests otherwise. The GC will eventually be run, bringing the heap size back to where it started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.