Skip to content

Instantly share code, notes, and snippets.

@lukehedger
Last active August 29, 2015 14:17
Show Gist options
  • Save lukehedger/7eda739de2ba253b88b1 to your computer and use it in GitHub Desktop.
Save lukehedger/7eda739de2ba253b88b1 to your computer and use it in GitHub Desktop.
Monitor JS garbage collection
// run this in Chrome with the --enable-precise-memory-info flag
// $ open -a "Google Chrome" --args --enable-precise-memory-info
timer = function() {
requestAnimationFrame(timer)
var heapBefore = window.performance.memory.usedJSHeapSize
// run processes here
// eg. console.log allocates 2216 bytes, new Object() 56 bytes
var heapAfter = window.performance.memory.usedJSHeapSize;
// account for 40 bytes allocated to run this function
var heapDiff = (heapAfter - heapBefore) - 40;
// heapDiff < 0 indicates a garbage collection occurred
if (heapDiff < 0) {
console.log("gc:", heapDiff);
}
}
requestAnimationFrame(timer)
@lukehedger
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment