node/test/parallel/test-diagnostics-channel-memory-leak.js
Joyee Cheung 7f2d61f82a
v8: implement v8.queryObjects() for memory leak regression testing
This is similar to the `queryObjects()` console API provided by the
Chromium DevTools console. It can be used to search for objects that
have the matching constructor on its prototype chain in the entire
heap, which can be useful for memory leak regression tests. To avoid
surprising results, users should avoid using this API on constructors
whose implementation they don't control, or on constructors that can
be invoked by other parties in the application.

To avoid accidental leaks, this API does not return raw references to
the objects found. By default, it returns the count of the objects
found. If `options.format` is `'summary'`, it returns an array
containing brief string representations for each object. The visibility
provided in this API is similar to what the heap snapshot provides,
while users can save the cost of serialization and parsing and directly
filer the target objects during the search.

We have been using this API internally for the test suite, which
has been more stable than any other leak regression testing
strategies in the CI. With a public implementation we can now
use the public API instead.

PR-URL: https://github.com/nodejs/node/pull/51927
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2024-03-02 22:11:30 +00:00

23 lines
590 B
JavaScript

// Flags: --max-old-space-size=16
'use strict';
// This test ensures that diagnostic channel references aren't leaked.
const common = require('../common');
const { subscribe, unsubscribe, Channel } = require('diagnostics_channel');
const { checkIfCollectableByCounting } = require('../common/gc');
function noop() {}
const outer = 64;
const inner = 256;
checkIfCollectableByCounting((i) => {
for (let j = 0; j < inner; j++) {
const key = String(i * inner + j);
subscribe(key, noop);
unsubscribe(key, noop);
}
return inner;
}, Channel, outer).then(common.mustCall());