MCPcopy Index your code
hub / github.com/nodejs/node / onGC

Function onGC

test/common/gc.js:19–41  ·  view source on GitHub ↗

* Installs a garbage collection listener for the specified object. * Uses async_hooks for GC tracking, which may affect test functionality. * A full setImmediate() invocation passes between a global.gc() call and the listener being invoked. * @param {object} obj - The target object to track for g

(obj, gcListener)

Source from the content-addressed store, hash-verified

17 * @param {Function} gcListener.ongc - The function to call when the target object is garbage collected.
18 */
19function onGC(obj, gcListener) {
20 const async_hooks = require('async_hooks');
21
22 const onGcAsyncHook = async_hooks.createHook({
23 init: common.mustCallAtLeast(function(id, type) {
24 if (this.trackedId === undefined) {
25 assert.strictEqual(type, gcTrackerTag);
26 this.trackedId = id;
27 }
28 }),
29 destroy(id) {
30 assert.notStrictEqual(this.trackedId, -1);
31 if (id === this.trackedId) {
32 this.gcListener.ongc();
33 onGcAsyncHook.disable();
34 }
35 },
36 }).enable();
37 onGcAsyncHook.gcListener = gcListener;
38
39 gcTrackerMap.set(obj, new async_hooks.AsyncResource(gcTrackerTag));
40 obj = null;
41}
42
43/**
44 * Repeatedly triggers garbage collection until a specified condition is met or a maximum number of attempts is reached.

Calls 4

createHookMethod · 0.80
requireFunction · 0.50
enableMethod · 0.45
setMethod · 0.45

Tested by 1

testFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…