Open In App

Node.js os.freemem() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The os.freemem() method is an inbuilt application programming interface of the os module which is used to get the amount of free system memory. 

Syntax:

os.freemem()

Parameters: This method does not accept any parameters. 

Return Value: This method returns an integer value that specifies the amount of free system memory in bytes. 

Example 1: The below example illustrates the use of os.freemem() method in Node.js: 

javascript




// Node.js program to demonstrate the
// os.freemem() method
 
// Allocating os module
const os = require('os');
 
// Printing os.freemem() value
console.log(os.freemem());


Output:

4158910464

Example 2: The below example illustrates the use of os.freemem() method in Node.js: 

javascript




// Node.js program to demonstrate the
// os.freemem() method
 
// Allocating os module
const os = require('os');
 
// Printing os.freemem() value
const free_memory = os.freemem();
const free_mem_in_kb = free_memory / 1024;
const free_mem_in_mb = free_mem_in_kb / 1024;
const 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");


Output:

Free memory: 4GB 110MB 88KB and 0Bytes

Note: The above program will compile and run by using the node index.js command. 

Reference: https://nodejs.org/api/os.html#os_os_freemem



Last Updated : 11 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads