Skip to content

Commit 0e7004b

Browse files
committed
doc: add heap profiler labels API documentation
Document the new labels API on startSamplingHeapProfiler, getAllocationProfile, withHeapProfileLabels, and setHeapProfileLabels. Signed-off-by: Rudolf Meijering <[email protected]>
1 parent c1934a5 commit 0e7004b

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

doc/api/v8.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,108 @@ added:
14531453
14541454
Returns true if the Node.js instance is run to build a snapshot.
14551455
1456+
## Heap profile labels
1457+
1458+
<!-- YAML
1459+
added: REPLACEME
1460+
-->
1461+
1462+
> Stability: 1 - Experimental
1463+
1464+
Attach string labels to V8 sampling heap profiler allocation samples.
1465+
Combined with [`AsyncLocalStorage`][], labels propagate through `await`
1466+
boundaries for per-context memory attribution (e.g., per-HTTP-route).
1467+
1468+
### `v8.startSamplingHeapProfiler([sampleInterval[, stackDepth[, options]]])`
1469+
1470+
<!-- YAML
1471+
added: REPLACEME
1472+
-->
1473+
1474+
* `sampleInterval` {number} Average interval in bytes. **Default:** `524288`.
1475+
* `stackDepth` {number} Maximum call stack depth. **Default:** `16`.
1476+
* `options` {Object}
1477+
* `includeCollectedObjects` {boolean} Retain samples for GC'd objects.
1478+
**Default:** `false`.
1479+
1480+
Starts the V8 sampling heap profiler.
1481+
1482+
### `v8.stopSamplingHeapProfiler()`
1483+
1484+
<!-- YAML
1485+
added: REPLACEME
1486+
-->
1487+
1488+
Stops the sampling heap profiler and clears all registered label entries.
1489+
1490+
### `v8.getAllocationProfile()`
1491+
1492+
<!-- YAML
1493+
added: REPLACEME
1494+
-->
1495+
1496+
* Returns: {Object | undefined}
1497+
1498+
Returns the current allocation profile, or `undefined` if the profiler is
1499+
not running.
1500+
1501+
```json
1502+
{
1503+
"samples": [
1504+
{ "nodeId": 1, "size": 128, "count": 4, "sampleId": 42,
1505+
"labels": { "route": "/users/:id" } }
1506+
]
1507+
}
1508+
```
1509+
1510+
* `samples[].labels` — key-value string pairs from the active label context
1511+
at allocation time. Empty object if no labels were active.
1512+
1513+
### `v8.withHeapProfileLabels(labels, fn)`
1514+
1515+
<!-- YAML
1516+
added: REPLACEME
1517+
-->
1518+
1519+
* `labels` {Object} Key-value string pairs (e.g., `{ route: '/users/:id' }`).
1520+
* `fn` {Function} May be `async`.
1521+
* Returns: {*} Return value of `fn`.
1522+
1523+
Runs `fn` with the given labels active. If `fn` returns a promise, labels
1524+
remain active until the promise settles.
1525+
1526+
```js
1527+
v8.startSamplingHeapProfiler(64);
1528+
1529+
await v8.withHeapProfileLabels({ route: '/users' }, async () => {
1530+
const data = await fetchUsers();
1531+
return processData(data);
1532+
});
1533+
1534+
const profile = v8.getAllocationProfile();
1535+
v8.stopSamplingHeapProfiler();
1536+
```
1537+
1538+
### `v8.setHeapProfileLabels(labels)`
1539+
1540+
<!-- YAML
1541+
added: REPLACEME
1542+
-->
1543+
1544+
* `labels` {Object} Key-value string pairs.
1545+
1546+
Sets labels for the current async scope using `enterWith` semantics.
1547+
Useful for frameworks where the handler runs after the extension returns.
1548+
1549+
Prefer [`v8.withHeapProfileLabels()`][] when possible for automatic cleanup.
1550+
1551+
### Limitations — what is measured
1552+
1553+
Heap samples cover V8 heap allocations (JS objects, strings, closures).
1554+
1555+
Not measured: `Buffer`/`ArrayBuffer` backing stores, native addon memory,
1556+
JIT code space, OS-level allocations.
1557+
14561558
## Class: `v8.GCProfiler`
14571559
14581560
<!-- YAML
@@ -1798,7 +1900,10 @@ console.log(profile);
17981900
[`serializer.transferArrayBuffer()`]: #serializertransferarraybufferid-arraybuffer
17991901
[`serializer.writeRawBytes()`]: #serializerwriterawbytesbuffer
18001902
[`settled` callback]: #settledpromise
1903+
[`v8.setHeapProfileLabels()`]: #v8setheapprofilelabelslabels
18011904
[`v8.stopCoverage()`]: #v8stopcoverage
18021905
[`v8.takeCoverage()`]: #v8takecoverage
1906+
[`v8.withHeapProfileLabels()`]: #v8withheapprofilelabelslabels-fn
1907+
[Limitations — what is measured]: #limitations--what-is-measured
18031908
[`vm.Script`]: vm.md#new-vmscriptcode-options
18041909
[worker threads]: worker_threads.md

0 commit comments

Comments
 (0)