Node.js - os.totalmem() 方法



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

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

语法

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


 os.totalmem()

参数

此方法不接受任何参数。

返回值

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

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

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


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

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

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

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

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

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

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