Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chandlerprall/10150810 to your computer and use it in GitHub Desktop.
Save chandlerprall/10150810 to your computer and use it in GitHub Desktop.
WeakMap values hanging around
wm = new WeakMap();
gc(); // Mark-sweep 2.6 (40.3) -> 2.2 (40.3) MB
// Starting memory usage is 40.3 MB
keys = [];
for ( i = 0; i < 1000000; i++ ) {
var key = {};
keys.push( key );
wm.set( key, true );
}
gc(); // Mark-sweep 105.3 (144.3) -> 105.2 (144.3) MB
// Memory usage with keys + values is 144.3 MB
// Remove keys, let them get collected
keys.length = 0;
gc(); // Mark-sweep 105.2 (144.3) -> 34.2 (77.4) MB
// Keys have been collected, now down to 77.4 MB... which means values are still around
// Force collect the WeakMap
wm = null;
gc(); // Mark-sweep 2.2 (41.3) -> 2.2 (41.3) MB
// Back down to the low 40s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment