Skip to content
Merged
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
14 changes: 13 additions & 1 deletion ts/src/heap-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {isMainThread} from 'worker_threads';

let enabled = false;
let heapIntervalBytes = 0;
let heapStackDepth = 0;
let startedWithAllocations = false;

/*
Expand Down Expand Up @@ -86,12 +87,22 @@ export function profile(
sourceMapper?: SourceMapper,
generateLabels?: GenerateAllocationLabelsFunction,
): Profile {
return convertProfile(
const result = convertProfile(
v8Profile(),
ignoreSamplePath,
sourceMapper,
generateLabels,
);
// In allocation mode V8 keeps every sampled object (live + collected-by-GC)
// in an append-only buffer that's only freed by stopSamplingHeapProfiler.
// Without this reset, each call to getAllocationProfile re-reads the entire
// history, so alloc_* totals grow linearly with wall-clock time and V8's
// internal sample buffer leaks for the lifetime of the process.
if (startedWithAllocations) {
stopSamplingHeapProfiler();
startSamplingHeapProfiler(heapIntervalBytes, heapStackDepth, true);
}
return result;
}

export function convertProfile(
Expand Down Expand Up @@ -193,6 +204,7 @@ export function start(
);
}
heapIntervalBytes = intervalBytes;
heapStackDepth = stackDepth;
startedWithAllocations = allocations;
startSamplingHeapProfiler(intervalBytes, stackDepth, allocations);
enabled = true;
Expand Down
Loading