1// Flags: --expose-gc 2'use strict'; 3 4const common = require('../common'); 5const assert = require('assert'); 6const { 7 PerformanceObserver, 8 constants 9} = require('perf_hooks'); 10 11const { 12 NODE_PERFORMANCE_GC_MAJOR, 13 NODE_PERFORMANCE_GC_MINOR, 14 NODE_PERFORMANCE_GC_INCREMENTAL, 15 NODE_PERFORMANCE_GC_WEAKCB, 16 NODE_PERFORMANCE_GC_FLAGS_FORCED 17} = constants; 18 19const kinds = [ 20 NODE_PERFORMANCE_GC_MAJOR, 21 NODE_PERFORMANCE_GC_MINOR, 22 NODE_PERFORMANCE_GC_INCREMENTAL, 23 NODE_PERFORMANCE_GC_WEAKCB, 24]; 25 26// Adding an observer should force at least one gc to appear 27{ 28 const obs = new PerformanceObserver(common.mustCallAtLeast((list) => { 29 const entry = list.getEntries()[0]; 30 assert(entry); 31 assert.strictEqual(entry.name, 'gc'); 32 assert.strictEqual(entry.entryType, 'gc'); 33 assert(kinds.includes(entry.kind)); 34 assert.strictEqual(entry.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED); 35 assert.strictEqual(typeof entry.startTime, 'number'); 36 assert.strictEqual(typeof entry.duration, 'number'); 37 obs.disconnect(); 38 })); 39 obs.observe({ entryTypes: ['gc'] }); 40 global.gc(); 41 // Keep the event loop alive to witness the GC async callback happen. 42 setImmediate(() => setImmediate(() => 0)); 43} 44 45// GC should not keep the event loop alive 46{ 47 let didCall = false; 48 process.on('beforeExit', () => { 49 assert(!didCall); 50 didCall = true; 51 global.gc(); 52 }); 53} 54