Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bindings/profilers/heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ NAN_METHOD(HeapProfiler::StartSamplingHeapProfiler) {
#if NODE_MAJOR_VERSION >= 26
if (allocations) {
flags = static_cast<v8::HeapProfiler::SamplingFlags>(
v8::HeapProfiler::kSamplingForceGC |
v8::HeapProfiler::kSamplingIncludeObjectsCollectedByMajorGC |
v8::HeapProfiler::kSamplingIncludeObjectsCollectedByMinorGC);
}
Expand Down
5 changes: 2 additions & 3 deletions ts/src/heap-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,8 @@ export function profileV2(
* @param intervalBytes - average number of bytes between samples.
* @param stackDepth - maximum stack depth for samples collected.
* @param allocations - include total allocation samples in the profile.
* When true, each call to `profile()` forces a full GC (via
* v8::HeapProfiler::kSamplingForceGC) to classify sampled objects as
* live/dead, which adds a measurable pause. Requires Node.js >= 26.
* Retains samples of objects collected by GC. Liveness reflects the most
* recent GC; collecting a profile does not force one. Requires Node.js >= 26.
*/
export function start(
intervalBytes: number,
Expand Down
27 changes: 26 additions & 1 deletion ts/test/test-heap-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
AllocationProfileNodeWithStats,
LabelSet,
} from '../src/v8-types';
import {fork} from 'child_process';
import {execFileSync, fork} from 'child_process';
import path from 'path';
import fs from 'fs';

Expand Down Expand Up @@ -330,6 +330,31 @@ describe('HeapProfiler', () => {
});
});

it('should collect allocation profiles without forcing GC', function () {
this.timeout(30000);
if (Number(process.versions.node.split('.')[0]) < 26) this.skip();

const output = execFileSync(
process.execPath,
[
'--trace-gc',
'-e',
`
const heap = require(${JSON.stringify(require.resolve('../src/heap-profiler'))});
heap.start(512 * 1024, 64, true);
global.retained = Array.from({length: 200000}, (_, i) => ({i}));
console.time('10 allocation profiles');
for (let i = 0; i < 10; i++) heap.profile();
console.timeEnd('10 allocation profiles');
heap.stop();
`,
],
{encoding: 'utf8', timeout: 25000},
);
console.log(output.trim());
assert.doesNotMatch(output, /sampling profiler/);
});

describe('foreign heap sampler', () => {
// Regression test: V8's sampling heap profiler can be enabled by something
// other than pprof (inspector, DevTools, another agent). getAllocationProfile
Expand Down
Loading