node-weak comes in! This module exports V8's
optionally attach a callback function to any arbitrary JS object. The callback
function will be invoked right before the Object is garbage collected (i.e. after
there are no more remaining references to the Object in JS-land).
This module can, for example, be used for debugging; to determine whether or not an Object is being garbage collected as it should. Take a look at the example below for commented walkthrough scenario.
$ npm install weak
Here's an example of calling a
cleanup() function on a Object before it gets
var weak = require'weak'// we are going to "monitor" this Object and invoke "cleanup"// before the object is garbage collectedvar obj =a: truefoo: 'bar'// The function to call before Garbage Collection.// Note that by the time this is called, 'obj' has been set to `null`.delete oadelete ofoo// Here's where we set up the weak referencevar ref = weakobj// `this` inside the callback is the EventEmitter.// The first argument to the callback is the `obj`.// DO NOT store any new references to the object, and// DO NOT use the object in any async functions.cleanupobj// While `obj` is alive, `ref` proxies everything to it, so:refa === objareffoo === objfoo// Clear out any references to the object, so that it will be GC'd at some point...obj = null////// Time passes, and the garbage collector is run//// `callback()` above is called, and `ref` now acts like an empty object.typeof reffoo === 'undefined'
It's important to be careful when using the "callbacks" feature of
otherwise you can end up in a situation where the watched object will never
be garbage collected.
You should not define the callback function in the same scope as the object that is being watched. It's often best to define the callback function at the highest scope possible (top-level being the best). Named functions work really well for this:
var http = require'http'weak = require'weak'httpcreateServerweakreq gcReqweakres gcResresend'Hello World\n'listen3000console.log'GC\'d `req` object'console.log'GC\'d `res` object'
The main exports is the function that creates the weak reference.
The first argument is the Object that should be monitored.
The Object can be a regular Object, an Array, a Function, a RegExp, or any of
the primitive types or constructor function created with
Optionally, you can set a callback function to be invoked
before the object is garbage collected. The callback will
be invoked with
obj as its first argument.
get() returns the actual reference to the Object that this weak reference was
created with. If this is called with a dead reference,
undefined is returned.
Checks to see if
ref is a dead reference. Returns
true if the original Object
has already been GC'd,
Checks to see if
ref is "near death". This will be
true exactly during the
weak reference callback function, and
false any other time.
Checks to see if
obj is "weak reference" instance. Returns
true if the
passed in object is a "weak reference",
callback to the Array of callback functions that will be invoked before the
Object gets garbage collected. The callbacks get executed in the order that they
Returns the internal
ref iterates through to invoke the GC
callbacks. The array can be
unshift()ed onto, to have more control
over the execution order of the callbacks. This is similar in concept to node's