Node.js | os.freemem() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

4158910464

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.