title: PM2 - Incorrect memory usage reading & possible memory leak with Node.js application
body: <p>When my node js server is running via pm2, it has a higher memory usage reading than the actual memory heap in the application when inspected in DevTools. More so, the value under <code>memory</code> in pm2 slowly increases over time, possibly indicating some kind of memory leak. This slow increase in memory usage also cannot be observed in DevTools.</p>
  
  <p>Any explanation and/or solutions to these two (seemingly) strange occurrences?</p>
  
  <p>This is my DevTools</p>
  
  <p><a href="https://i.stack.imgur.com/J7EJ2.png" rel="noreferrer"><img src="https://i.stack.imgur.com/J7EJ2.png" alt="enter image description here"></a></p>
  
  <p>This is <code>pm2 list</code></p>
  
  <p><a href="https://i.stack.imgur.com/PgLdL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PgLdL.png" alt="enter image description here"></a></p>
  
  <p>here is my <code>javascript</code> code</p>
  
  <pre><code>var SSE = require('sse');
  var https = require('https');
  var fs = require('fs');
  var url = require('url');
  var mysql = require('mysql');
  var schedule = require('node-schedule');
  
  var options = {
      key: fs.readFileSync('pathto/ssl.key'),
      cert: fs.readFileSync('pathto/ssl.crt'),
      ca: fs.readFileSync('pathto/ssl.ca-bundle')
  };
  
  
  var pool = mysql.createPool({
      connectionLimit: 100,
      host: "host",
      user: "user",
      password: "pass",
      database: "db"
  });
  
  async function connectandrun() {
      try {
  
          var server = https.createServer(options, function(req, res) {
              var queryData = url.parse(req.url, true).query;
              res.writeHead(200, {
                  'Content-Type': 'text/event-stream',
                  'Access-Control-Allow-Origin': '*',
                  'Cache-Control': 'no-cache',
                  'Connection': 'keep-alive',
                  'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
                  'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
              });
  
              if (queryData.connectionid) {
                  var connecitonid = queryData.connectionid;
              } else {
                  var connecitonid = "";
              }
  
              var myconection = "myconnecction" + connecitonid;
              var uuserjson = {};
              uuserjson[myconection] = {
                  Data: {
                      Element: null
                  }
              };
  
              schedule.scheduleJob('*/3 * * * * *', function() {
                  var runninginstance = main(uuserjson, queryData, myconection, res).catch(console.error);
                  runninginstance = null;
              });
  
              res.on("close", function() {
                  res.end();
                  uuserjson[myconection] = null;
                  myconection = null;
                  connecitonid = null;
              });
          });
  
          server.listen(3000, '0.0.0.0', function() {
              var sse = new SSE(server);
              sse.on('connection', function(client) {
                  client.send('hi there!');
              });
          });
  
      } finally {}
  }
  
  connectandrun().catch(console.error);
  
  async function main(uuserjson, queryData, myconection, res) {
  
  
      pool.getConnection(function(err, con) {
          if (err) {
              console.log(err);
          } else {
              con.query("MYSQL QUERY",
                  function(err, result, fields) {
                      if (err) throw err;
                      if (result.length != 0) {
                          uuserjson[myconection] = {
                              Data: {
                                  Element: result[0]
                              }
                          };
  
                          if (result[0]) {
                              res.write("retry: 30000\n\n" + "event: blanks\ndata: " + result[0] + "\n\n");
                          }
                      }
  
                      con.release();
                  });
          }
      });
  
  }
  </code></pre>