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
Recommended Posts:
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs console.timeLog() Method
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Web-Socket in NodeJS
- Signup Form Using Nodejs and MongoDB
- Dockerizing a simple Nodejs app
- Nodejs | DNS
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Nodejs | Jimp
- Nodejs – Connect Mysql with Node app
- Nodejs | DNS | setServers()
- NodeJS | Building simple REST API in express
- NodeJs - Handling invalid routes
- Nodejs | authentication using Passportjs and passport-local-mongoose
- Nodejs | GM charcoal() Function
- Nodejs | GM blur() Function
- Nodejs | GM sharpen() Function
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.

