• Node.js Video Tutorials

Node.js - os.totalmem() 方法



Node.js 的os 模块提供了一组与操作系统相关的实用方法和属性。

os 模块的Node.js os.totalmem() 方法返回系统总内存量,以整数形式表示的字节数。

语法

以下是Node.js os.totalmem() 方法的语法:

os.totalmem()

参数

此方法不接受任何参数。

返回值

此方法返回系统总内存量(以字节为单位)的整数。

以下示例演示了 os 模块中 Node.js os.totalmem() 方法的用法。

示例

在以下示例中,我们导入 os 模块并尝试通过将Node.js os.totalmem() 方法记录到控制台来打印当前系统的总内存。

const os = require('os');
console.log(os.totalmem());

输出

134948720640

注意 - 为了获得准确的结果,最好在本地执行以上代码。

执行上述程序后,os.totalmem() 方法将返回当前系统总 RAM 内存(以字节为单位)。

16822587392

示例

在以下示例中,我们将系统总内存量从字节转换为 KB(千字节)、MB(兆字节)和 GB(吉字节)。

const os = require('os');

var tot_mem_in_bytes = os.totalmem();
var tot_mem_in_kb = Math.floor(os.totalmem()/(1024));
var tot_mem_in_mb = Math.floor(os.totalmem()/(1024*1024));
var tot_mem_in_gb = Math.floor(os.totalmem()/(1024*1024*1024));

console.log("The amount of total current system memory (in bytes): " + tot_mem_in_bytes + " Bytes");
console.log("The amount of total current system memory (in KB): " + tot_mem_in_kb + " KB"); 
console.log("The amount of total current system memory (in MB): " + tot_mem_in_mb + " MB"); 
console.log("The amount of total current system memory (in GB): " + tot_mem_in_gb + " GB");

输出

The amount of total current system memory (in bytes): 134948720640 Bytes
The amount of total current system memory (in KB): 131785860 KB
The amount of total current system memory (in MB): 128697 MB
The amount of total current system memory (in GB): 125 GB

注意 - 为了获得准确的结果,最好在本地执行以上代码。

如果我们编译并运行上述程序,我们将打印系统总 RAM 内存(以字节、千字节 (KB)、兆字节 (MB) 和吉字节 (GB) 为单位)。

The amount of total current system memory (in bytes): 16822587392 Bytes
The amount of total current system memory (in KB): 16428308 KB
The amount of total current system memory (in MB): 16043 MB
The amount of total current system memory (in GB): 15 GB

示例

在以下示例中,我们尝试将系统总内存和可用系统内存从字节转换为 KB(千字节)、MB(兆字节)和 GB(吉字节)。

const os = require('os');
var tot_mem_in_bytes = os.totalmem();
var tot_mem_in_kb = Math.floor(os.totalmem()/(1024));
var tot_mem_in_mb = Math.floor(os.totalmem()/(1024*1024));
var tot_mem_in_gb = Math.floor(os.totalmem()/(1024*1024*1024));

var mem_in_bytes = os.freemem();
var mem_in_kb = Math.floor(os.freemem()/(1024));
var mem_in_mb = Math.floor(os.freemem()/(1024*1024));
var mem_in_gb = Math.floor(os.freemem()/(1024*1024*1024));

console.log("The amount of total current system memory (in bytes): " + tot_mem_in_bytes + " Bytes");
console.log("The amount of total current system memory (in KB): " + tot_mem_in_kb + " KB"); 
console.log("The amount of total current system memory (in MB): " + tot_mem_in_mb + " MB"); 
console.log("The amount of total current system memory (in GB): " + tot_mem_in_gb + " GB");
console.log('----------------------------------------------------------------------------');

console.log("The amount of free current system memory (in bytes): " + mem_in_bytes + " Bytes");
console.log("The amount of free current system memory (in KB): " + mem_in_kb + " KB"); 
console.log("The amount of free current system memory (in MB): " + mem_in_mb + " MB"); 
console.log("The amount of free current system memory (in GB): " + mem_in_gb + " GB");

输出

The amount of total current system memory (in bytes): 134948720640 Bytes
The amount of total current system memory (in KB): 131785860 KB
The amount of total current system memory (in MB): 128697 MB
The amount of total current system memory (in GB): 125 GB
-----------------------------------------------------------------------
The amount of free current system memory (in bytes): 130385920000 Bytes
The amount of free current system memory (in KB): 127330000 KB
The amount of free current system memory (in MB): 124345 MB
The amount of free current system memory (in GB): 121 GB

注意 - 为了获得准确的结果,最好在本地执行以上代码。

执行上述程序后,我们将打印系统总 RAM 内存和可用 RAM 内存(以字节、千字节 (KB)、兆字节 (MB) 和吉字节 (GB) 为单位)。

The amount of total current system memory (in bytes): 16822587392 Bytes
The amount of total current system memory (in KB): 16428308 KB
The amount of total current system memory (in MB): 16043 MB
The amount of total current system memory (in GB): 15 GB
----------------------------------------------------------------------------
The amount of free current system memory (in bytes): 8955355136 Bytes
The amount of free current system memory (in KB): 8745464 KB
The amount of free current system memory (in MB): 8540 MB
The amount of free current system memory (in GB): 8 GB
nodejs_os_module.htm
广告