Skip to content
Related Articles

Related Articles

Node.js os.freemem() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Oct, 2021
View Discussion
Improve Article
Save Article

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.

Below examples illustrate the use of os.freemem() method in Node.js:

Example 1:




// 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:




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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!