Node.js os.freemem() 方法
os.freemem() 方法是 os 模块的内置应用程序编程接口,用于获取可用系统内存的数量。
句法:
os.freemem()
参数:此方法不接受任何参数。
返回值:此方法返回一个整数值,以字节为单位指定可用系统内存量。
下面的例子说明了 Node.js 中os.freemem() 方法的使用:
示例 1:
// Node.js program to demonstrate the
// os.freemem() method
// Allocating os module
const os = require('os');
// Printing os.freemem() value
console.log(os.freemem());
输出:
4158910464
示例 2:
// Node.js program to demonstrate the
// os.freemem() method
// Allocating os module
const os = require('os');
// Printing os.freemem() value
var free_memory = os.freemem();
var free_mem_in_kb = free_memory/1024;
var free_mem_in_mb = free_mem_in_kb/1024;
var free_mem_in_gb = free_mem_in_mb/1024;
free_mem_in_kb = Math.floor(free_mem_in_kb);
free_mem_in_mb = Math.floor(free_mem_in_mb);
free_mem_in_gb = Math.floor(free_mem_in_gb);
free_mem_in_mb = free_mem_in_mb%1024;
free_mem_in_kb = free_mem_in_kb%1024;
free_memory = free_memory%1024;
console.log("Free memory: " + free_mem_in_gb + "GB "
+ free_mem_in_mb + "MB " + free_mem_in_kb
+ "KB and " + free_memory + "Bytes");
输出:
Free memory: 4GB 110MB 88KB and 0Bytes
注意:以上程序将使用node index.js
命令编译运行。
参考: https://nodejs.org/api/os.html#os_os_freemem