// File: example.js var key = {}; var indirectReference = {}; indirectReference['key'] = (function() { var m = {}; m['foo'] = 'bar'; return m; })(); var map = new WeakMap(); map.set(key, indirectReference['key']); console.log('Has key after setting value: %s', map.has(key)); delete indirectReference['key']; console.log('Has key after deleting value: %s', map.has(key)); global.gc(); console.log('Has key after performing global.gc(): %s', map.has(key));I downloaded the latest version of io.js (so that I could have a JavaScript runtime with
WeakMap
and global.gc()
and ran it as follows:
./iojs --expose-gc example.jsHere is what I see:
Has key after setting value: true Has key after deleting value: true Has key after performing global.gc(): trueDespite my best efforts, I can't seem to get
WeakMap
to give up the value that is mapped to key
. Am I doing it wrong? Obviously I'm making some assumptions here, so I'm curious where I'm off.
Ultimately, I would like to be able to use WeakMap
to write some tests to ensure certain objects get garbage collected and don't leak memory.