• 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
廣告
© . All rights reserved.