Node Js Check Computer RAM Memory Size Tutorial

In this simple yet informative tutorial, we are going to find out how to check the total and available computer system RAM (Random Access Memory) memory size in the Node js application.

To inspect computer memory size in node, we will use the node js os module.

The os constructor function offers tons of properties and methods that may help you access the operating system-related information.

Specifically, we will use the os.freemem() method; the freemem method helps you know the amount of free system memory in bytes as an integer.

Furthermore, we will also use the totalmem() method via the os module. The totalmem method allows you to check the total amount of computer system memory in bytes within the node environment.

From scratch, we will explain how to create a basic node app, set up a server file, and write code to know the total amount of RAM of a working computer. learn to use the

Let’s start coding.

How to Get System RAM Memory Size in Node Js

  • Step 1: Generate Folder
  • Step 2: Create Project Package File
  • Step 3: Create App Js File
  • Step 4: Check Memory Size
  • Step 5: Test Node App

Generate Folder

We need to generate a new folder to keep the code of our node feature.

There are two ways to create a new folder; either you can make it manually or use the command.

mkdir node-fetch

Go inside the project’s directory

cd node-fetch

Create Project Package File

We are now ready to initialize the node project, and it requires an npm init command to run from the terminal’s console panel.

As soon as the above command is evoked, the command line interface will ask you to fill in some information regarding your node project.

npm init

Here is the new package.json file that generated after running the above command.

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Create App Js File

Inside your node project folder, you have to make a new file. You can name it whatever you want. We chose to name it app.js file.

Now, straight go to the package.json file, inside the script property append the app.js name right after the node attribute in start property.

  "scripts": {
    "start": "node app.js"
  },

Check Memory Size

In this step, you need to go to the app.js file, open the file and insert all the given code into the file.

let os = require('os')

let totalRAM = os.totalmem()
let totalMem = (totalRAM / (1024 * 1024))
console.log(`Total available RAM: ${totalMem}`)


let freeRAM = os.freemem()
let totalFreeMem = (freeRAM / (1024 * 1024))
console.log(`Total free system RAM: ${totalFreeMem}`)

Test Node App

You are now eventually ready to find out how much system memory you have left and the total memory.

node app.js

Let’s just one more time invoke the following command and display the output on the console.

# Total available RAM: 16384

# Total free system RAM: 4645.50390625

Summary

Node Js Check Computer RAM Memory Size Tutorial

In this quick tutorial, we have learned and comprehended how to profoundly check the amount of free system RAM in bytes in the Node app.

Moreover, we also figured out how to get the total amount of system memory in bytes in a node application.