@@ -295,6 +295,55 @@ console.log(globalVar);
295295// 1000
296296```
297297
298+ ## ` vm.measureMemory([options, [contextifiedObject]]) `
299+
300+ <!-- YAML
301+ added: REPLACEME
302+ -->
303+
304+ > Stability: 1 - Experimental
305+
306+ Measure the memory used by the current execution context or a specified context.
307+
308+ * ` options ` {Object} Optional.
309+ * ` mode ` {vm.constants.measureMemory.mode}
310+ ** Default:** ` vm.constants.measureMemory.mode.SUMMARY `
311+ * ` contextifiedObject ` {Object} Optional. A [ contextified] [ ] object returned
312+ by ` vm.createContext() ` . If not specified, measure the memory usage of the
313+ current context where ` vm.measureMemory() ` is invoked.
314+ * Returns: {Promise} If the memory is successfully measured the promise will
315+ resolve with an object containing information about the memory usage.
316+
317+ The format of the object that the returned Promise may resolve with is
318+ specific to the V8 engine and may change from one version of V8 to the next.
319+
320+ The returned result is different from the statistics returned by
321+ ` v8.GetHeapSpaceStatistics() ` in that ` vm.measureMemory() ` measures
322+ the memory reachable from a specific context, while
323+ ` v8.GetHeapSpaceStatistics() ` measures the memory used by an instance
324+ of V8 engine, which can switch among multiple contexts that reference
325+ objects in the heap of one engine.
326+
327+ ``` js
328+ const vm = require (' vm' );
329+ // Measure the memory used by the current context and return the result
330+ // in summary.
331+ vm .measureMemory ({ mode: vm .constants .measureMemory .mode .SUMMARY })
332+ // Is the same as vm.measureMemory()
333+ .then ((result ) => {
334+ // The current format is:
335+ // { total: { jsMemoryEstimate: 2211728, jsMemoryRange: [ 0, 2211728 ] } }
336+ console .log (result);
337+ });
338+
339+ const context = vm .createContext ({});
340+ vm .measureMemory ({ mode: vm .constants .measureMemory .mode .DETAILED }, context)
341+ .then ((result ) => {
342+ // At the moment the DETAILED format is the same as the SUMMARY one.
343+ console .log (result);
344+ });
345+ ```
346+
298347## Class: ` vm.Module `
299348<!-- YAML
300349added: v13.0.0
@@ -1169,6 +1218,26 @@ the `process.nextTick()` and `queueMicrotask()` functions.
11691218This issue occurs because all contexts share the same microtask and nextTick
11701219queues.
11711220
1221+ ## ` vm.constants`
1222+ <!-- YAML
1223+ added: REPLACEME
1224+ -->
1225+
1226+ * {Object } An object containing commonly used constants for the vm module .
1227+
1228+ ### ` vm.constants.measureMemory`
1229+ <!-- YAML
1230+ added: REPLACEME
1231+ -->
1232+
1233+ > Stability: 1 - Experimental
1234+
1235+ Constants to be used with the [` vm.measureMemory()` ][] method.
1236+
1237+ * ` mode` {Object }
1238+ * ` SUMMARY` {integer} Return the measured memory in summary.
1239+ * ` DETAILED` {integer} Return the measured memory in detail.
1240+
11721241[` ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING` ]: errors .html #ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING
11731242[` ERR_VM_MODULE_STATUS` ]: errors .html #ERR_VM_MODULE_STATUS
11741243[` Error` ]: errors .html #errors_class_error
@@ -1178,6 +1247,7 @@ queues.
11781247[` script.runInThisContext()` ]: #vm_script_runinthiscontext_options
11791248[` url.origin` ]: url .html #url_url_origin
11801249[` vm.createContext()` ]: #vm_vm_createcontext_contextobject_options
1250+ [` vm.measureMemory()` ]: #vm_vm_measurememory_options_contextifiedobject
11811251[` vm.runInContext()` ]: #vm_vm_runincontext_code_contextifiedobject_options
11821252[` vm.runInThisContext()` ]: #vm_vm_runinthiscontext_code_options
11831253[Cyclic Module Record]: https: // tc39.es/ecma262/#sec-cyclic-module-records
0 commit comments