我在node.js中有以下代码。它是一个长期运行的后台服务
如果我在我的Mac上运行它,内存使用率在30M以下相当稳定。但是当我在CENTOS服务器上运行时,内存消耗不断增加。我对node.js相当新。 有人可以指导我吗?这是因为递归吗?
var nodemailer = require('../lib/nodemailer'),
fs = require("fs"),
pathlib = require("path");
var mysql = require("mysql");
// Create an Amazon SES transport object
var transport = nodemailer.createTransport("SES", {
AWSAccessKeyID: "xxxxxxx",
AWSSecretKey: "yyyyyyyyy",
ServiceUrl: "https://email.us-east-1.amazonaws.com" // optional
});
console.log('SES Configured');
var connection = mysql.createConnection({
user: "user",
password: "xxxxxx",
database: "db_content"
});
// Message object
function loop(){
console.log("Reading from DB");
connection.query('SELECT * FROM mm_messages where status = 0 limit 1;', function (error, rows, fields) {
if( rows.length == 1 ) {
var message = {
from: 'abc <abc@xxxxx.com>',
to: rows[0]["to"],
subject: rows[0]["subject"] + rows[0]["id"], //
text: 'Hello to myself!',
html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'
};
console.log('Sending Mail');
transport.sendMail(message, function(error){
if(error){
console.log('Error occured');
console.log(error.message);
//return;
}
console.log('Message sent successfully!');
connection.query('update mm_messages set status = 1 where id='+rows[0]["id"] + ';', function (errorq, rowsq, fieldsq) {
message = null;
error = null;
errorq = null;
rowsq = null;
rows = null;
fieldsq = null;
fields = null;
setTimeout(loop, 2000);
});
});
} else {
message = null;
rows = null;
fields =null;
error= null;
setTimeout(loop, 2000);
}
});
}
loop();
答案 0 :(得分:0)
对我而言,这一切看起来都很温和。以下是您可以更改然后重新测量的一些内容。
connection.query('SELECT * FROM mm_messages where status = 0 limit 1;', function (error, rows, fields) {
connection.query('SELECT * FROM mm_messages where status = 0 limit 1;', messageCallback
foo = null;
语句。它们是不必要的。它们是单一的。它们使您的代码比需要的时间更长。它们是由FUD驱动的,而不是具有实际目的。