Created
April 8, 2014 16:21
-
-
Save chandlerprall/10150810 to your computer and use it in GitHub Desktop.
WeakMap values hanging around
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Proper test is to use a real value in the map instead of
true
, see https://code.google.com/p/v8/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Priority%20Owner%20Summary%20HW%20OS%20Area%20Stars&groupby=&sort=&id=3262