简体   繁体   中英

How do I get the instance memory in use for a Node-Express App running in CloudFoundry?

I am currently building an app with a node-express backend and deploying it to CloudFoundry. I know I can get the total instance memory from the VCAP_APPLICATION var and I am able to get the OS memory using the 'os' module. It seems that I can get the application memory using the 'util' module, but none of the parameters of the returned object from process.memoryUsage() seem to be what I'm looking for. I am doing the following to get all that:

// Modules I've tried
var express = require('express'),
    app = express(),
    os = require("os"),
    util = require("util");

// Memory statistics I can get -> cannot get instance memory in use
var memLimit = JSON.parse(process.env.VCAP_APPLICATION)['limits']['mem'];
    totalMem = os.totalmem(),
    freeMem = os.freemem(),
    memUsed = util.inspect(process.memoryUsage()).split(" "),
    heapUsed = memUsed[6],
    heapTotal = memUsed[4].substr(0,memUsed[4].length-1);
    rss = memUsed[2].substr(0,memUsed[2].length-1);

Does anyone know how I can use the heap/rss values or any other modules to get what I am looking for?

I can think of two ways of potentially accomplishing this.

If you don't necessarily need to get this value within your app, the cf utility can tell you how much memory your process is using.

Running cf app <app-name> will provide stats about the given app, one of which is memory usage.

In the case that you need this from your app code, you could try the node-memwatch package. It can periodically emit events with stats about memory usage.

You could also take a look at the code node-memwatch uses to see how they compute the values.

Be careful, though. Memory profiling can be expensive, so watch what and how it's implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM