Memory leak
Hi,
I'am building a 'slave checker' in node. Basically what it does is query all the slaves in our replication cluster to check their seconds behind master, master state and a query from information_schema. The meaning of this node is to keep running so the statuses can get requested from the node and are not done by our backend every single time(since information schema queries are becoming a bit to heavy and is a waste of resource)
These two queries are done every second and the memory keeps increasing everytime a query is executed. I reproduced this with the following code:
var mysql = require('mysql');
var mysqlClient = mysql.createConnection({
host: 'hostname',
user: 'username',
password: 'password'
});
setInterval(function(){
var myQuery = 'SHOW SLAVE STATUS;';
mysqlClient.query(query, function(err, results) {
//doesn't matter
});
var query = 'SELECT TIME, TIME_MS FROM information_schema.processlist WHERE USER = "system user" AND STATE NOT LIKE "waiting for%" AND STATE NOT LIKE "queueing master%" ORDER BY ID DESC LIMIT 1';
mysqlClient.query(query, function(err, results) {
//also doesn't matter
});
}, 500);
If you just set the interval lower the memory will build op more quickly. I don't think there's a way for me to fix this but it's in the MySql code. If you need any more information feel free to contact me :)
What version of node-mysql are you using? You may be interested in https://github.com/felixge/node-mysql/issues/471
How long does it take to execute those two queries? Could it be, that your query queue grows larger and larger, because the DB-server is too slow answering? I have no experience with clusters, but doesn't it take time to ask the slaves over the network about their status? If you increase the interval, does it reach a point where it does not leak anymore?
OS info: Distributor ID: Debian Description: Debian GNU/Linux 6.0.6 (squeeze) Release: 6.0.6 Codename: squeeze
I use the alpha7 at first then check if alpha8 still had the problem, both was the case.
node version: v0.10.2
Queries on information schema only take several ms unless there are some locks on it then it takes the time of the other query but this never happens on our cluster. All network data is going through backend which means no high data transfers could interfer on this one plus it's a 1gbps network. Our MySql servers can be called high-end so the weak link is not there. If you want I can send you more specific data on response times. The increased interval doesn't make it leak less, it just goes slower until it reaches the point of abnormal. with an interval of 1 second it took a weekend to get upto 2GB of memory usage. We have another case where nodejs tracks our webserverload and also this script has some memoryleaks in it with mysql included.
Thanks for spending time on this issue. If you need any more details let me know and if you want more direct contact you're free to email me at [email protected]
@hemera1337 you are saying alpha7 has the issue as well? Are you sure?
If that's the case, then your problem must be unrelated to #471.
I use alpha7 to, and I have leak. What version should I use?
@hemera1337 I'm running your code snippet for hours now (locally though - no network latency) and everything seems to be just fine: I was setting your timer even to 50 ms and memory consumption hovers around 50 MB (with alpha 7 + 8 [unfixed]). I also noticed that the column TIME_MS doesn't exist in processlist in MySQL. Are you using MariaDB or Percona Server?
Nonetheless, if I could make some suggestions:
You should definitely 'wait' for the callback to keep processing your results, and then spin off another timer; your approach in general can be very dangerous for all kinds of reasons. If you set the timer value lower it can/will be even worse ...
If you need both results at the same time, you could use the functionality for multiple queries:
connection.query('SELECT 1; SELECT 2', function(err, results) {
if (err) throw err;
// `results` is an array with one element for every statement in the query:
console.log(results[0]); // [{1: 1}]
console.log(results[1]); // [{2: 2}]
});
if you don't like it, you could use nested statements:
connection.query('SELECT 1', function(err, results) {
connection.query('SELECT 2', function(err2, results2) {
// .. do something with the results here
});
});
@itopenup Do you have a repro?
Daniel
@daniel-76 Very simple:
var io = require('socket.io');
var mysql = require('mysql');
....................
var connection = mysql.createConnection({
user: 'root',
password: 'SjT1daUHYM6VzAZKzYQj',
host: 'localhost',
database: 'pointax'
});
connection.connect();
var onPing = function () {
//console.log('Mem: ' + util.inspect(process.memoryUsage()));
this.emit('ping', {result: 1});
connection.query('INSERT INTO driver_ping(driver_id, x, y, time) VALUES(1, 1, 1, NOW())');
}
var onConnect = function (socket) {
socket.on('ping', onPing);
console.log('Connected');
}
io.sockets.on('connection', onConnect);
This is all my script.
And I send 'ping' event for socket.io every second.
Ubuntu "top" command shows node uses about 50 Mb RES memory, buffers memory grows in 1 Mb every few minutes.

@daniel-76 We are running percona 5.5
I just run the same code again but with console.log(process.memoryUsage()) every 2 seconds. this is the data for one minute:
Alpha8: { rss: 17092608, heapTotal: 12376832, heapUsed: 5413032 } { rss: 17764352, heapTotal: 16571136, heapUsed: 5193536 } { rss: 19271680, heapTotal: 16571136, heapUsed: 7553600 } { rss: 21188608, heapTotal: 16571136, heapUsed: 7584224 } { rss: 24514560, heapTotal: 17603072, heapUsed: 8149912 } { rss: 24723456, heapTotal: 17603072, heapUsed: 10435016 } { rss: 25890816, heapTotal: 27023616, heapUsed: 10957576 } { rss: 27643904, heapTotal: 27023616, heapUsed: 13192544 } { rss: 30109696, heapTotal: 27023616, heapUsed: 15492984 } { rss: 33239040, heapTotal: 30119424, heapUsed: 13868784 } { rss: 33980416, heapTotal: 30119424, heapUsed: 5723896 } { rss: 34062336, heapTotal: 30119424, heapUsed: 7977696 } { rss: 34148352, heapTotal: 30119424, heapUsed: 10101440 } { rss: 34263040, heapTotal: 30119424, heapUsed: 12525352 } { rss: 34811904, heapTotal: 30119424, heapUsed: 10154296 } { rss: 36540416, heapTotal: 30119424, heapUsed: 12373320 } { rss: 39813120, heapTotal: 46896640, heapUsed: 11165832 } { rss: 39907328, heapTotal: 46896640, heapUsed: 13439384 } { rss: 39989248, heapTotal: 46896640, heapUsed: 15697128 } { rss: 41725952, heapTotal: 46896640, heapUsed: 17984072 } { rss: 44216320, heapTotal: 46896640, heapUsed: 20235272 } { rss: 46559232, heapTotal: 46896640, heapUsed: 22491328 } { rss: 50941952, heapTotal: 48960512, heapUsed: 17700112 } { rss: 51032064, heapTotal: 48960512, heapUsed: 19968816 } { rss: 49868800, heapTotal: 48960512, heapUsed: 6391696 } { rss: 49881088, heapTotal: 48960512, heapUsed: 8690224 } { rss: 49901568, heapTotal: 48960512, heapUsed: 10887544 } { rss: 49975296, heapTotal: 48960512, heapUsed: 13154952 } { rss: 50057216, heapTotal: 48960512, heapUsed: 15403864 } { rss: 50143232, heapTotal: 48960512, heapUsed: 17674968 } { rss: 50229248, heapTotal: 48960512, heapUsed: 19890000 } { rss: 51073024, heapTotal: 48960512, heapUsed: 12882960 }
Alpha7: { rss: 16998400, heapTotal: 11344896, heapUsed: 5487344 } { rss: 17616896, heapTotal: 11344896, heapUsed: 4363288 } { rss: 17850368, heapTotal: 12376832, heapUsed: 5683912 } { rss: 18210816, heapTotal: 16571136, heapUsed: 4916600 } { rss: 19234816, heapTotal: 16571136, heapUsed: 7965872 } { rss: 20852736, heapTotal: 16571136, heapUsed: 7180152 } { rss: 23126016, heapTotal: 16571136, heapUsed: 6380528 } { rss: 23314432, heapTotal: 16571136, heapUsed: 9421576 } { rss: 23642112, heapTotal: 16571136, heapUsed: 8611912 } { rss: 23953408, heapTotal: 16571136, heapUsed: 7839048 } { rss: 24276992, heapTotal: 16571136, heapUsed: 7015104 } { rss: 24395776, heapTotal: 16571136, heapUsed: 9997280 } { rss: 24715264, heapTotal: 16571136, heapUsed: 9237728 } { rss: 23166976, heapTotal: 16571136, heapUsed: 5974944 } { rss: 23252992, heapTotal: 16571136, heapUsed: 5094096 } { rss: 23347200, heapTotal: 16571136, heapUsed: 8098920 } { rss: 23564288, heapTotal: 16571136, heapUsed: 7385200 } { rss: 23695360, heapTotal: 16571136, heapUsed: 6606424 } { rss: 23912448, heapTotal: 16571136, heapUsed: 5750040 } { rss: 24002560, heapTotal: 16571136, heapUsed: 8753088 } { rss: 24137728, heapTotal: 16571136, heapUsed: 7982928 } { rss: 24264704, heapTotal: 16571136, heapUsed: 7076312 } { rss: 24567808, heapTotal: 24959744, heapUsed: 6328680 } { rss: 24653824, heapTotal: 24959744, heapUsed: 9241976 } { rss: 26832896, heapTotal: 24959744, heapUsed: 12228368 } { rss: 29028352, heapTotal: 24959744, heapUsed: 7507328 } { rss: 29368320, heapTotal: 24959744, heapUsed: 10447840 } { rss: 32374784, heapTotal: 24959744, heapUsed: 13444016 } { rss: 33882112, heapTotal: 25991680, heapUsed: 8894936 } { rss: 33959936, heapTotal: 25991680, heapUsed: 11848304 } { rss: 34033664, heapTotal: 25991680, heapUsed: 14800648 } { rss: 34508800, heapTotal: 25991680, heapUsed: 10164968 } { rss: 34635776, heapTotal: 25991680, heapUsed: 13153632 }
Hi @daniel-76 do you have any status update on this?
@hemera1337 Sorry, not yet. I tried a couple things, but I didn't get anywhere. I want to designate some time for it on the weekend ...
Sorry for bothering you but did you find any solution? @daniel-76
@hemera1337 Sorry, unfortunately I didn't find any issues. I'd like to really encourage you though to NOT spin up potentially 'unlimited' function calls in a timer ... Let the callback spin up another call, you are better off that way!
I'm having memory leak problems too, so I had a look at your code. I've tried a reworked version of it: calling the second query in the callback of the first one:
var cnt = 0;
query1()
function query1() {
var myQuery = 'SHOW SLAVE STATUS;';
var q = mysqlClient.query(myQuery, function(err, results) {
// console.log('here', err, results)
query2()
});
q = null
}
function query2() {
var query = 'SELECT TIME FROM information_schema.processlist WHERE USER = "system user" AND STATE NOT LIKE "waiting for%" AND STATE NOT LIKE "queueing master%" ORDER BY ID DESC LIMIT 1';
var q = mysqlClient.query(query, function(err, results) {
// console.log('there', err, results)
setTimeout(function() { query1() }, 100)
if (cnt % 50 == 0)
// display rss memory usage
console.log(process.memoryUsage())
cnt++
});
q = null
}
Result: there does not seem to be a memory leak. rss keeps increasing until it reaches about 50M, then stays steady.
If you stop to query then the memory is very slow to be released though (like 10kb every 40 seconds for rss)
I'm going to close this for now. @hemera1337 please post some JavaScript I can copy-and-paste into a file and run that demonstrates the memory leak. The original post (with empty callback functions) does not reproduce the memory leak, so it's possible the contents of those functions do matter. Your second example uses socket.io which introduces something that may itself be leaking or leaking due to the usage there. Please only use the mysql module as a dependency in the example script so we can eliminate any outside interference.