1// Flags: --expose-gc --no-warnings 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(kinds.includes(entry.detail.kind)); 35 assert.strictEqual(entry.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED); 36 assert.strictEqual(entry.detail.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED); 37 assert.strictEqual(typeof entry.startTime, 'number'); 38 assert(entry.startTime < 1e4, 'startTime should be relative to performance.timeOrigin.'); 39 assert.strictEqual(typeof entry.duration, 'number'); 40 obs.disconnect(); 41 })); 42 obs.observe({ entryTypes: ['gc'] }); 43 global.gc(); 44 // Keep the event loop alive to witness the GC async callback happen. 45 setImmediate(() => setImmediate(() => 0)); 46} 47 48// GC should not keep the event loop alive 49{ 50 let didCall = false; 51 process.on('beforeExit', () => { 52 assert(!didCall); 53 didCall = true; 54 global.gc(); 55 }); 56} 57