Node.js V8模块

  • V8
    • v8.getHeapStatistics()
    • v8.getHeapSpaceStatistics()
    • v8.setFlagsFromString(string)

The v8 module exposes APIs that are specific to the version of V8 built into the Node.js binary. It can be accessed using:

v8模块暴露的应用程序接口由编译到Node.js的二进制文件中的V8的版本号指定。可以用该方法进行范围:

const v8 = require('v8');

Note: The APIs and implementation are subject to change at any time.

注意: 这些应用程序接口和实现方式,随时有可能被改变。

v8.getHeapStatistics()

Added in: v1.0.0

Returns an object with the following properties:

  • total_heap_size
  • total_heap_size_executable
  • total_physical_size
  • total_available_size
  • used_heap_size
  • heap_size_limit
const v8 = require('v8');
v8.getHeapStatistics();
/*
返回
{ 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 }
*/

这里写图片描述

v8.getHeapSpaceStatistics()

Added in: v6.0.0

Returns statistics about the V8 heap spaces, i.e. the segments which make up 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.

返回V8堆空间,就是组成V8堆的片段的统计信息。通过V8的GetHeapSpaceStatistics函数获得的堆空间的顺序或者堆空间的可用信息都不能保证可以作为可靠的统计信息,因为但V8的版本发生改变时它们也可能改变。

堆和栈的区别

  1. heap是堆,stack是栈。
  2. stack的空间由操作系统自动分配和释放,heap的空间是手动申请和释放的,heap常用new关键字来分配。
  3. stack空间有限,heap的空间是很大的自由区。在Java中,若只是声明一个对象,则先在栈内存中为其分配地址空间,若再new一下,实例化它,则在堆内存中为其分配地址。

例如:数据类型 变量名;这样定义的东西在栈区。如:Object a =null; 只在栈内存中分配空间new 数据类型();或者malloc(长度); 这样定义的东西就在堆区如:Object b =new Object(); 则在堆内存中分配空间。

The value returned is an array of objects containing the following properties:

该返回值是一个对象数组,包含如下属性:

  • space_name
  • space_size
  • space_used_size
  • space_available_size
  • physical_space_size
const v8 = require('v8');
v8.getHeapStackStatistics();
/*
返回
[ { "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 } ]
*/

这里写图片描述

v8.getHeapStatistics()与v8.getHeapSpaceStatistics()的关系


var stat = v8.getHeapStatistics();
var spaceStat = v8.getHeapSpaceStatistics();

得到
stat.total_heap_size = stat.total_physical_size = AVERAGE( spaceStat.space_size ) = AVERAGE( spaceStat.physical_space_size )

v8.setFlagsFromString(string)

Added in: v1.0.0

The v8.setFlagsFromString() method can be used to programmatically set V8 command line flags. This method should be used with care. Changing settings after the VM has started may result in unpredictable behavior, including crashes and data loss; or it may simply do nothing.

The V8 options available for a version of Node.js may be determined by running node –v8-options. An unofficial, community-maintained list of options and their effects is available here.

v8.setFlagsFromString()函数可以用来程序化的设置V8的命令行标志。该函数必须小心使用。在虚拟机启动后改变配置可能导致不可预测的行为,包括崩溃和数据丢失,或者也可能简单的什么都不做。

当前版本的Node.js的V8的选项可以用node –v8-options命令查看。一个非官方的,社区持有的选项列表和它们的作用在此
https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md (Node.js v6.6.0)

const v8 = require('v8');
v8.setFlagsFromString('--trace_gc');
setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值