2014-12-09 22:57:48 +01:00
|
|
|
# V8
|
|
|
|
|
2016-05-18 07:11:57 +02:00
|
|
|
The `v8` module exposes APIs that are specific to the version of [V8][]
|
|
|
|
built into the Node.js binary. It can be accessed using:
|
2014-12-09 22:57:48 +01:00
|
|
|
|
2016-05-18 07:11:57 +02:00
|
|
|
```js
|
|
|
|
const v8 = require('v8');
|
|
|
|
```
|
|
|
|
|
|
|
|
*Note*: The APIs and implementation are subject to change at any time.
|
2014-12-09 22:57:48 +01:00
|
|
|
|
2017-02-23 08:04:48 +01:00
|
|
|
## v8.cachedDataVersionTag()
|
|
|
|
<!-- YAML
|
|
|
|
added: REPLACEME
|
|
|
|
-->
|
|
|
|
|
|
|
|
Returns an integer representing a "version tag" derived from the V8 version,
|
|
|
|
command line flags and detected CPU features. This is useful for determining
|
|
|
|
whether a [`vm.Script`][] `cachedData` buffer is compatible with this instance
|
|
|
|
of V8.
|
|
|
|
|
2016-05-06 08:36:37 +02:00
|
|
|
## v8.getHeapSpaceStatistics()
|
2016-05-11 05:21:30 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.0.0
|
2017-02-21 23:38:50 +01:00
|
|
|
changes:
|
|
|
|
- version: v7.5.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10186
|
|
|
|
description: Support values exceeding the 32-bit unsigned integer range.
|
2016-05-11 05:21:30 +02:00
|
|
|
-->
|
2015-12-29 11:54:35 +01:00
|
|
|
|
|
|
|
Returns statistics about the V8 heap spaces, i.e. the segments which make up
|
2016-05-18 07:11:57 +02:00
|
|
|
the V8 heap. Neither the ordering of heap spaces, nor the availability of a
|
|
|
|
heap space can be guaranteed as the statistics are provided via the V8
|
|
|
|
[`GetHeapSpaceStatistics`][] function and may change from one V8 version to the
|
|
|
|
next.
|
|
|
|
|
|
|
|
The value returned is an array of objects containing the following properties:
|
|
|
|
* `space_name` {string}
|
|
|
|
* `space_size` {number}
|
|
|
|
* `space_used_size` {number}
|
|
|
|
* `space_available_size` {number}
|
|
|
|
* `physical_space_size` {number}
|
2015-12-29 11:54:35 +01:00
|
|
|
|
2016-05-18 07:11:57 +02:00
|
|
|
For example:
|
2015-12-29 11:54:35 +01:00
|
|
|
|
2016-07-09 07:13:09 +02:00
|
|
|
```json
|
2015-12-29 11:54:35 +01:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"space_name": "new_space",
|
|
|
|
"space_size": 2063872,
|
|
|
|
"space_used_size": 951112,
|
|
|
|
"space_available_size": 80824,
|
|
|
|
"physical_space_size": 2063872
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"space_name": "old_space",
|
|
|
|
"space_size": 3090560,
|
|
|
|
"space_used_size": 2493792,
|
|
|
|
"space_available_size": 0,
|
|
|
|
"physical_space_size": 3090560
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"space_name": "code_space",
|
|
|
|
"space_size": 1260160,
|
|
|
|
"space_used_size": 644256,
|
|
|
|
"space_available_size": 960,
|
|
|
|
"physical_space_size": 1260160
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"space_name": "map_space",
|
|
|
|
"space_size": 1094160,
|
|
|
|
"space_used_size": 201608,
|
|
|
|
"space_available_size": 0,
|
|
|
|
"physical_space_size": 1094160
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"space_name": "large_object_space",
|
|
|
|
"space_size": 0,
|
|
|
|
"space_used_size": 0,
|
|
|
|
"space_available_size": 1490980608,
|
|
|
|
"physical_space_size": 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
2017-02-13 03:49:35 +01:00
|
|
|
## v8.getHeapStatistics()
|
|
|
|
<!-- YAML
|
|
|
|
added: v1.0.0
|
2017-02-21 23:38:50 +01:00
|
|
|
changes:
|
|
|
|
- version: v7.2.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/8610
|
|
|
|
description: Added `malloced_memory`, `peak_malloced_memory`,
|
|
|
|
and `does_zap_garbage`.
|
|
|
|
- version: v7.5.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10186
|
|
|
|
description: Support values exceeding the 32-bit unsigned integer range.
|
2017-02-13 03:49:35 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
Returns an object with the following properties:
|
|
|
|
|
|
|
|
* `total_heap_size` {number}
|
|
|
|
* `total_heap_size_executable` {number}
|
|
|
|
* `total_physical_size` {number}
|
|
|
|
* `total_available_size` {number}
|
|
|
|
* `used_heap_size` {number}
|
|
|
|
* `heap_size_limit` {number}
|
|
|
|
* `malloced_memory` {number}
|
|
|
|
* `peak_malloced_memory` {number}
|
|
|
|
* `does_zap_garbage` {number}
|
|
|
|
|
|
|
|
`does_zap_garbage` is a 0/1 boolean, which signifies whether the `--zap_code_space`
|
|
|
|
option is enabled or not. This makes V8 overwrite heap garbage with a bit
|
|
|
|
pattern. The RSS footprint (resident memory set) gets bigger because it
|
|
|
|
continuously touches all heap pages and that makes them less likely to get
|
|
|
|
swapped out by the operating system.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
```js
|
|
|
|
{
|
|
|
|
total_heap_size: 7326976,
|
|
|
|
total_heap_size_executable: 4194304,
|
|
|
|
total_physical_size: 7326976,
|
|
|
|
total_available_size: 1152656,
|
|
|
|
used_heap_size: 3476208,
|
|
|
|
heap_size_limit: 1535115264,
|
|
|
|
malloced_memory: 16384,
|
|
|
|
peak_malloced_memory: 1127496,
|
|
|
|
does_zap_garbage: 0
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-05-06 08:36:37 +02:00
|
|
|
## v8.setFlagsFromString(string)
|
2016-05-11 05:21:30 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.0.0
|
|
|
|
-->
|
2014-12-09 22:57:48 +01:00
|
|
|
|
2016-11-15 18:56:15 +01:00
|
|
|
The `v8.setFlagsFromString()` method can be used to programmatically set
|
2016-05-18 07:11:57 +02:00
|
|
|
V8 command line flags. This method should be used with care. Changing settings
|
2014-12-09 22:57:48 +01:00
|
|
|
after the VM has started may result in unpredictable behavior, including
|
2016-05-18 07:11:57 +02:00
|
|
|
crashes and data loss; or it may simply do nothing.
|
2014-12-09 22:57:48 +01:00
|
|
|
|
2015-12-07 20:20:50 +01:00
|
|
|
The V8 options available for a version of Node.js may be determined by running
|
2015-08-13 18:14:34 +02:00
|
|
|
`node --v8-options`. An unofficial, community-maintained list of options
|
2015-11-14 04:21:49 +01:00
|
|
|
and their effects is available [here][].
|
2014-12-09 22:57:48 +01:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2014-12-09 22:57:48 +01:00
|
|
|
// Print GC events to stdout for one minute.
|
2015-12-15 00:20:25 +01:00
|
|
|
const v8 = require('v8');
|
2014-12-09 22:57:48 +01:00
|
|
|
v8.setFlagsFromString('--trace_gc');
|
|
|
|
setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
|
|
|
|
```
|
|
|
|
|
2016-03-02 20:16:24 +01:00
|
|
|
[V8]: https://developers.google.com/v8/
|
2017-02-23 08:04:48 +01:00
|
|
|
[`vm.Script`]: vm.html#vm_new_vm_script_code_options
|
2015-11-14 04:21:49 +01:00
|
|
|
[here]: https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md
|
2016-05-18 07:11:57 +02:00
|
|
|
[`GetHeapSpaceStatistics`]: https://v8docs.nodesource.com/node-5.0/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4
|