Skip to content

Instantly share code, notes, and snippets.

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