user10044012
user10044012

Reputation:

Get the remaining amount of memory in percentage with node

I am using node and I have some values for memory:

var freeMemory = os.freemem();
var totalMemory = os.totalmem();

I don't know if any more values are needed.

My question is...how can I get the freeMemory in percentage?

Upvotes: 2

Views: 1141

Answers (2)

TGrif
TGrif

Reputation: 5931

You have the value of free memory and the value of available memory, so it is simple as a percentage calculation:

console.log(Math.round((freeMemory * 100) / totalMemory) + '%')  // => 68%

Upvotes: 1

Shubham Dixit
Shubham Dixit

Reputation: 1

Use os-utils library

    npm install os-utils
    var os  = require('os-utils');


    os.cpuUsage(function(v){
        console.log( 'CPU Usage (%): ' + v );
    });

    os.cpuFree(function(v){
        console.log( 'CPU Free:' + v ); 
    });

    console.log(os.freememPercentage());//gets you the free memory percentage

For more visit the docs here-https://github.com/oscmejia/os-utils

Upvotes: 1

Related Questions