问题描述
当我的节点 js 服务器通过 pm2 运行时,在 DevTools 中检查时,它的内存使用率读数高于应用程序中的实际内存堆.更重要的是,pm2 中 memory
下的值随着时间的推移缓慢增加,可能表明某种内存泄漏.在 DevTools 中也无法观察到这种内存使用量的缓慢增加.
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 memory
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.
对这两个(看似)奇怪的事件有任何解释和/或解决方案吗?
Any explanation and/or solutions to these two (seemingly) strange occurrences?
这是我的开发工具
这是pm2列表
这是我的javascript
代码
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();
});
}
});
}
推荐答案
在与 OP 合作后,已确认 PM2
中存在某种内存泄漏.
After teaming up with OP on this, it has been confirmed there is some sort of memory leak in PM2
.
请参阅以下有关我们调查结果的文章":
Please see the below 'write-up' on our findings:
问题:
- 随着时间的推移,
PM2
使用越来越多的 RAM- 这表明存在某种内存泄漏
- Slowly over time
PM2
uses more and more RAM- This points to some sort of memory leak
证据&谣言:
- 在没有
PM2
的情况下运行应用程序时,只使用node myserver.js
,没有证据表明 RAM 随着时间的推移缓慢增加- 内存保持平稳
- 用于测试该理论的应用程序是一个小型 Web 应用程序,以尽量减少代码包含内存泄漏的可能性,而该泄漏实际上来自
PM2
- When running the application without
PM2
, just usingnode myserver.js
, there is no evidence of RAM slowly increasing over time- RAM remains flat
- The application that was used to test this theory is a small web app, in order to minimize the chance that the code contains a memory leak, and that the leak is in fact coming from
PM2
- 查看最后一条评论
PM2
未使用时不会发生这种情况
- That do not occur when
PM2
is NOT used
- 这个错误是在今年 [2019] 年提交的,在撰写本文时已于 10 月发表评论,其中有人描述了这仍然是一个问题
您应该考虑的 PM2 替代品:
Phusion Passenger
似乎是替换PM2
- `可在此处找到快速入门指南
- Phusion Passenger 看起来是最接近
PM2
的比较,我会从这里开始 - 不,我与 Phusion Passenger 没有任何关系...
Phusion Passenger
seems like a strong candidate for replacingPM2
- `Quick-start guide can be found here
- Phusion Passenger looks like the closest comparison to
PM2
and is where I would start - No, I do not have any affiliation with Phusion Passenger...
- 它似乎没有得到积极维护(在他们 GitHub 上提交的一些问题中,人们建议使用其他应用程序)
PM2
并不是真正的苹果对苹果",但是嘿,多样性是生活的调味品
- Not really 'apples to apples' with
PM2
, but hey, variety is the spice of life
- 似乎没有得到积极维护,并且在星级方面有点小"
PM2
解决方法/创可贴:(!不建议在生产中依赖这些!)- 设置内存阈值以自动重新加载
PM2
一旦消耗了 X 个 RAM- 这个特性"在
PM2
中是原生的
- Set a Memory Threshold to auto-reload
PM2
once X amount of RAM has been consumed- This 'feature' is native within
PM2
这篇关于PM2 - 不正确的内存使用读数 &Node.js 应用程序可能存在内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- This 'feature' is native within
- 这个特性"在