@@ -1453,6 +1453,113 @@ added:
14531453
14541454Returns 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+ " externalBytes" : [
1508+ { " labels" : { " route" : " /users/:id" }, " bytes" : 1048576 }
1509+ ]
1510+ }
1511+ ` ` `
1512+
1513+ * ` samples[].labels ` — key-value string pairs from the active label context
1514+ at allocation time. Empty object if no labels were active.
1515+ * ` externalBytes[]` — live ` Buffer` /` ArrayBuffer ` backing-store bytes per
1516+ label context. Complements heap samples which only see the JS wrapper.
1517+
1518+ ### ` v8 .withHeapProfileLabels (labels, fn)`
1519+
1520+ <!-- YAML
1521+ added: REPLACEME
1522+ -->
1523+
1524+ * ` labels` {Object} Key-value string pairs (e.g., ` { route: ' /users/:id' }` ).
1525+ * ` fn` {Function} May be ` async ` .
1526+ * Returns: {*} Return value of ` fn` .
1527+
1528+ Runs ` fn` with the given labels active. If ` fn` returns a promise, labels
1529+ remain active until the promise settles.
1530+
1531+ ` ` ` js
1532+ v8 .startSamplingHeapProfiler (64 );
1533+
1534+ await v8 .withHeapProfileLabels ({ route: ' /users' }, async () => {
1535+ const data = await fetchUsers ();
1536+ return processData (data);
1537+ });
1538+
1539+ const profile = v8 .getAllocationProfile ();
1540+ v8 .stopSamplingHeapProfiler ();
1541+ ` ` `
1542+
1543+ ### ` v8 .setHeapProfileLabels (labels)`
1544+
1545+ <!-- YAML
1546+ added: REPLACEME
1547+ -->
1548+
1549+ * ` labels` {Object} Key-value string pairs.
1550+
1551+ Sets labels for the current async scope using ` enterWith` semantics.
1552+ Useful for frameworks where the handler runs after the extension returns.
1553+
1554+ Prefer [` v8 .withHeapProfileLabels ()` ][] when possible for automatic cleanup.
1555+
1556+ ### Limitations — what is measured
1557+
1558+ Heap samples cover V8 heap allocations (JS objects, strings, closures).
1559+ ` externalBytes` covers ` Buffer` /` ArrayBuffer ` backing stores.
1560+
1561+ Not measured: native addon memory, JIT code space, OS-level allocations.
1562+
14561563## Class: ` v8 .GCProfiler `
14571564
14581565<!-- YAML
@@ -1798,7 +1905,10 @@ console.log(profile);
17981905[` serializer .transferArrayBuffer ()` ]: #serializertransferarraybufferid-arraybuffer
17991906[` serializer .writeRawBytes ()` ]: #serializerwriterawbytesbuffer
18001907[` settled` callback]: #settledpromise
1908+ [` v8 .setHeapProfileLabels ()` ]: #v8setheapprofilelabelslabels
18011909[` v8 .stopCoverage ()` ]: #v8stopcoverage
18021910[` v8 .takeCoverage ()` ]: #v8takecoverage
1911+ [` v8 .withHeapProfileLabels ()` ]: #v8withheapprofilelabelslabels-fn
1912+ [Limitations — what is measured]: #limitations--what-is-measured
18031913[` vm .Script ` ]: vm.md#new-vmscriptcode-options
18041914[worker threads]: worker_threads.md
0 commit comments