@@ -87,6 +87,24 @@ The value returned is an array of objects containing the following properties:
87
87
]
88
88
```
89
89
90
+ ## v8.getHeapSnapshot()
91
+ <!-- YAML
92
+ added: REPLACEME
93
+ -->
94
+
95
+ * Returns: {stream.Readable} A Readable Stream containing the V8 heap snapshot
96
+
97
+ Generates a snapshot of the current V8 heap and returns a Readable
98
+ Stream that may be used to read the JSON serialized representation.
99
+ This JSON stream format is intended to be used with tools such as
100
+ Chrome DevTools. The JSON schema is undocumented and specific to the
101
+ V8 engine, and may change from one version of V8 to the next.
102
+
103
+ ``` js
104
+ const stream = v8 .getHeapSnapshot ();
105
+ stream .pipe (process .stdout );
106
+ ```
107
+
90
108
## v8.getHeapStatistics()
91
109
<!-- YAML
92
110
added: v1.0.0
@@ -159,6 +177,58 @@ v8.setFlagsFromString('--trace_gc');
159
177
setTimeout (() => { v8 .setFlagsFromString (' --notrace_gc' ); }, 60e3 );
160
178
```
161
179
180
+ ## v8.writeHeapSnapshot([ filename] )
181
+ <!-- YAML
182
+ added: REPLACEME
183
+ -->
184
+
185
+ * ` filename ` {string} The file path where the V8 heap snapshot is to be
186
+ saved. If not specified, a file name with the pattern
187
+ ` 'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot' ` will be
188
+ generated, where ` {pid} ` will be the PID of the Node.js process,
189
+ ` {thread_id} ` will be ` 0 ` when ` writeHeapSnapshot() ` is called from
190
+ the main Node.js thread or the id of a worker thread.
191
+ * Returns: {string} The filename where the snapshot was saved.
192
+
193
+ Generates a snapshot of the current V8 heap and writes it to a JSON
194
+ file. This file is intended to be used with tools such as Chrome
195
+ DevTools. The JSON schema is undocumented and specific to the V8
196
+ engine, and may change from one version of V8 to the next.
197
+
198
+ A heap snapshot is specific to a single V8 isolate. When using
199
+ [ Worker Threads] [ ] , a heap snapshot generated from the main thread will
200
+ not contain any information about the workers, and vice versa.
201
+
202
+ ``` js
203
+ const { writeHeapSnapshot } = require (' v8' );
204
+ const {
205
+ Worker ,
206
+ isMainThread ,
207
+ parentPort
208
+ } = require (' worker_threads' );
209
+
210
+ if (isMainThread) {
211
+ const worker = new Worker (__filename );
212
+
213
+ worker .once (' message' , (filename ) => {
214
+ console .log (` worker heapdump: ${ filename} ` );
215
+ // Now get a heapdump for the main thread.
216
+ console .log (` main thread heapdump: ${ writeHeapSnapshot ()} ` );
217
+ });
218
+
219
+ // Tell the worker to create a heapdump.
220
+ worker .postMessage (' heapdump' );
221
+ } else {
222
+ parentPort .once (' message' , (message ) => {
223
+ if (message === ' heapdump' ) {
224
+ // Generate a heapdump for the worker
225
+ // and return the filename to the parent.
226
+ parentPort .postMessage (writeHeapSnapshot ());
227
+ }
228
+ });
229
+ }
230
+ ```
231
+
162
232
## Serialization API
163
233
164
234
> Stability: 1 - Experimental
@@ -417,4 +487,5 @@ A subclass of [`Deserializer`][] corresponding to the format written by
417
487
[ `vm.Script` ] : vm.html#vm_constructor_new_vm_script_code_options
418
488
[ HTML structured clone algorithm ] : https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
419
489
[ V8 ] : https://developers.google.com/v8/
490
+ [ Worker Threads ] : worker_threads.html
420
491
[ here ] : https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md
0 commit comments