• Node.js Video Tutorials

Node.js - os.totalmem() Method



The os module of Node.js provides a bunch of operating system-related utility methods and properties.

The Node.js os.totalmem() method of os module returns the total amount of system memory as a number of bytes in integer form.

Syntax

Following is the syntax of the Node.js os.totalmem() method −

os.totalmem()

Parameters

This method does not accept any parameters.

Return value

This method returns the total amount of system memory in bytes as an integer.

The below examples demonstrate the usage of Node.js os.totalmem() method of os module.

Example

In the following example, we are importing the os module and trying to print the total current system memory by logging Node.js os.totalmem() method to the console.

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

Output

134948720640

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the os.totalmem() method will return the current system's total RAM memory in bytes.

16822587392

Example

In the following example, we are converting the amount of total system memory from bytes to KB(Kilobyte), MB(Megabyte), and GB(Gigabyte).

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");

Output

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

Note − To get the accurate result, better execute the above code in local.

If we compile and run the above program, we printed the total system's RAM memory in bytes, kilobytes(KB), Megabytes(MB), and Gigabytes(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

Example

In the following example, we are trying the amount of total system memory and free system memory from bytes to KB(Kilobyte), MB(Megabyte), and GB(Gigabyte).

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");

Output

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

Note − To get the accurate result, better execute the above code in local.

After executing the above program, we printed the total and free system's RAM memory in bytes, kilobytes(KB), Megabytes(MB), and Gigabytes(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
Advertisements