@@ -1618,6 +1618,62 @@ added: v26.1.0
16181618
16191619> Stability: 1 - Experimental
16201620
1621+ ### ` syncHeapProfileHandle .getSamples ()`
1622+
1623+ <!-- YAML
1624+ added: REPLACEME
1625+ -->
1626+
1627+ * Returns: {Object\[ ]}
1628+ * ` size` {number} Estimated total allocation size in bytes, equal to
1629+ ` objectSize * objectCount` .
1630+ * ` objectSize` {number} Size of the sampled object in bytes.
1631+ * ` objectCount` {number} Estimated number of objects represented by the sample.
1632+ * ` nodeId` {number} Identifier of the allocation's node in the profile call tree.
1633+ * ` ordinal` {number} Unique, time-ordered identifier of the sample.
1634+ * ` isLive` {boolean} Whether the sampled object is still live.
1635+ * ` sampleInterval` {number} Sampling interval in bytes used when the sample
1636+ was selected.
1637+
1638+ Returns the currently retained allocation samples without stopping profiling or
1639+ building the full profile call tree and resolving its script metadata. The sample
1640+ fields match those in the JSON returned by ` stop ()` . Use ` ordinal` to identify
1641+ samples across calls; array order is unspecified.
1642+
1643+ Live allocation samples are available with the default profiling options.
1644+ To also retain samples after their objects are collected, enable
1645+ ` includeObjectsCollectedByMajorGC` or ` includeObjectsCollectedByMinorGC` when
1646+ starting the profile. These samples have ` isLive` set to ` false ` . The ` forceGC`
1647+ option also applies to ` getSamples ()` .
1648+
1649+ Throws ` ERR_HEAP_PROFILE_NOT_STARTED ` if this handle has been stopped or disposed.
1650+
1651+ ### ` syncHeapProfileHandle .setSampleInterval (sampleInterval)`
1652+
1653+ <!-- YAML
1654+ added: REPLACEME
1655+ -->
1656+
1657+ * ` sampleInterval` {integer} Average sampling interval in bytes. Must be between
1658+ ` 1 ` and ` Number .MAX_SAFE_INTEGER ` , inclusive.
1659+
1660+ Updates the sampling interval for future sample scheduling without restarting
1661+ profiling or discarding existing samples. A larger interval reduces sampling
1662+ frequency. Each sample retains the ` sampleInterval` used when it was selected.
1663+
1664+ Throws ` ERR_HEAP_PROFILE_NOT_STARTED ` if this handle has been stopped or disposed.
1665+
1666+ ` ` ` cjs
1667+ const v8 = require (' node:v8' );
1668+
1669+ const handle = v8 .startHeapProfile ({ sampleInterval: 1024 });
1670+ // Run the workload to profile.
1671+ const samples = handle .getSamples ();
1672+ handle .setSampleInterval (4096 );
1673+ // Continue profiling with less frequent sampling.
1674+ const profile = handle .stop ();
1675+ ` ` `
1676+
16211677### ` syncHeapProfileHandle .stop ()`
16221678
16231679<!-- YAML
@@ -1634,7 +1690,9 @@ fields, the following are added:
16341690
16351691* Each node reports ` selfCount` , the number of sampled objects allocated by the
16361692 node (` selfSize` is the existing total in bytes).
1637- * Each sample reports ` objectSize` , ` objectCount` , and ` isLive` .
1693+ * Each sample reports ` objectSize` , ` objectCount` , ` isLive` , and ` sampleInterval` .
1694+ ` sampleInterval` records the sampling interval in bytes used when the sample
1695+ was selected, even if the interval was subsequently changed.
16381696
16391697The DevTools ` size` field stays ` objectSize * objectCount` for compatibility.
16401698
@@ -1679,6 +1737,40 @@ Stopping collecting the profile and the profile will be discarded.
16791737added: v24.9.0
16801738-->
16811739
1740+ ### ` heapProfileHandle .getSamples ()`
1741+
1742+ <!-- YAML
1743+ added: REPLACEME
1744+ -->
1745+
1746+ * Returns: {Promise} Fulfills with an {Object\[ ]} of allocation samples.
1747+
1748+ Asynchronously retrieves the worker's retained allocation samples without
1749+ stopping profiling or building the full profile call tree. The sample fields and
1750+ profiling options are described in
1751+ [` syncHeapProfileHandle .getSamples ()` ][].
1752+
1753+ Rejects with ` ERR_HEAP_PROFILE_NOT_STARTED ` if this handle has been stopped or
1754+ disposed, or ` ERR_WORKER_NOT_RUNNING ` if the worker is no longer running.
1755+
1756+ ### ` heapProfileHandle .setSampleInterval (sampleInterval)`
1757+
1758+ <!-- YAML
1759+ added: REPLACEME
1760+ -->
1761+
1762+ * ` sampleInterval` {integer} Average sampling interval in bytes. Must be between
1763+ ` 1 ` and ` Number .MAX_SAFE_INTEGER ` , inclusive.
1764+ * Returns: {Promise} Fulfills with ` undefined ` after the interval is updated.
1765+
1766+ Asynchronously updates the worker's sampling interval for future sample
1767+ scheduling without restarting profiling or discarding existing samples. Each
1768+ sample retains the ` sampleInterval` used when it was selected.
1769+
1770+ Invalid arguments throw synchronously. The returned promise rejects with
1771+ ` ERR_HEAP_PROFILE_NOT_STARTED ` if this handle has been stopped or disposed, or
1772+ ` ERR_WORKER_NOT_RUNNING ` if the worker is no longer running.
1773+
16821774### ` heapProfileHandle .stop ()`
16831775
16841776<!-- YAML
@@ -1930,6 +2022,7 @@ must be a positive integer.
19302022[` serializer .transferArrayBuffer ()` ]: #serializertransferarraybufferid-arraybuffer
19312023[` serializer .writeRawBytes ()` ]: #serializerwriterawbytesbuffer
19322024[` settled` callback]: #settledpromise
2025+ [` syncHeapProfileHandle .getSamples ()` ]: #syncheapprofilehandlegetsamples
19332026[` v8 .setHeapSnapshotNearHeapLimit ()` ]: #v8setheapsnapshotnearheaplimitlimit
19342027[` v8 .startHeapProfile ()` ]: #v8startheapprofileoptions
19352028[` v8 .stopCoverage ()` ]: #v8stopcoverage
0 commit comments