-
-
Save TooTallNate/2381906 to your computer and use it in GitHub Desktop.
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
// run with "node --expose_gc" | |
var weak = require('weak') | |
, util = require('util') | |
var count = 0 | |
, countGc = 0 | |
function dec () { | |
count-- | |
} | |
function decGC () { | |
countGc-- | |
} | |
function test () { | |
function MyObject () { | |
var self = this; | |
Object.defineProperty(this, 'color', { | |
get: function() { return 'red' } | |
}) | |
setTimeout(function () { | |
self.emit('close') | |
}, 2000) | |
} | |
util.inherits(MyObject, process.EventEmitter) | |
for (var i = 0; i < 1000; i++) { | |
var o = new MyObject() | |
count++ | |
o.on('close', dec) | |
countGc++ | |
weak(o, decGC) | |
} | |
} | |
test() | |
setInterval(function () { | |
gc() | |
console.log('objects: %d, garbage collected: %d', count, countGc) | |
}, 1000) |
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
objects: 1000, garbage collected: 1000 | |
objects: 1000, garbage collected: 1000 | |
objects: 0, garbage collected: 12 | |
objects: 0, garbage collected: 12 | |
objects: 0, garbage collected: 12 | |
objects: 0, garbage collected: 12 | |
objects: 0, garbage collected: 12 | |
objects: 0, garbage collected: 12 | |
objects: 0, garbage collected: 12 | |
objects: 0, garbage collected: 12 | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment