mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
The native object behind histograms created by `createHistogram()`, `importHistogram()`, and `snapshot()` did not report the memory of its HDR histogram to V8. That memory is about 352 KiB with the default options, and V8 did not take it into account when scheduling garbage collection. Short-lived histograms could therefore hold on to a large amount of native memory until an unrelated garbage collection: in a loop taking 2,000 snapshots, RSS grew by 477 MiB. Report the size of the native histogram while the object is alive, as `SlidingWindowHistogram` already does for its chunks. Objects that share a native histogram after cloning each report its full size. With this change, RSS grows by 8 MiB in the same loop. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode PR-URL: https://github.com/nodejs/node/pull/66099 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
// Tests that histogram objects report the memory of their native histogram to
|
|
// V8, so that creating many short-lived histograms triggers garbage
|
|
// collection.
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { setImmediate: setImmediatePromise } = require('timers/promises');
|
|
const {
|
|
PerformanceObserver,
|
|
constants: { NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY },
|
|
createHistogram,
|
|
} = require('perf_hooks');
|
|
|
|
let externalMemoryGCs = 0;
|
|
const observer = new PerformanceObserver((list) => {
|
|
for (const entry of list.getEntries()) {
|
|
if (entry.detail.flags & NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY)
|
|
externalMemoryGCs++;
|
|
}
|
|
});
|
|
observer.observe({ entryTypes: ['gc'] });
|
|
|
|
// With the default options, a histogram holds 45,056 64-bit counts, about
|
|
// 352 KiB. V8 starts a garbage collection once external memory has grown by
|
|
// 64 MiB, so 400 unreferenced snapshots (about 137 MiB) must trigger one.
|
|
const histogram = createHistogram();
|
|
for (let i = 0; i < 400; i++) histogram.snapshot();
|
|
|
|
(async () => {
|
|
// Performance entries for garbage collections are delivered asynchronously.
|
|
for (let i = 0; i < 10 && externalMemoryGCs === 0; i++)
|
|
await setImmediatePromise();
|
|
observer.disconnect();
|
|
assert.ok(externalMemoryGCs > 0,
|
|
'Expected a garbage collection caused by external memory');
|
|
})().then(common.mustCall());
|