Not waiting for write callback leaks memory?
I'm working on very intense installation where I have to make a lot of writes very fast (up to 4000 writes @ 30fps).
Everything is working fine but the memory (RSS) is growing super fast. I tried batching (gluing buffers together and making less writes) and it helps a bit. I notice that the problem can be solved if I wait for every write to complete. I though that calling drain() now and then would be enough and I don't have to wait for writes and believe in async magic.
I'm using 1.3.0 on Windows.
Example code that can pump up RSS to 1GB in few min.
var SerialPort = require('serialport').SerialPort;
var portName = 'COM11';
var baudrate = 3000000;
var initialRSS = 0;
var frame = 0;
var sp = new SerialPort(portName, {
baudrate: baudrate,
buffersize: 255
}, false);
var data = [];
for(var i=0; i<100; i++) {
data.push(0);
}
var buf = new Buffer(data)
var onOpen = function (err) {
if (err) console.log(err);
else onFrame();
}
var onFrame = function () {
if (frame++ % 15 == 0) {
if (initialRSS == 0) {
initialRSS = Math.floor(process.memoryUsage().rss/100000)/10
}
console.log('rss', Math.floor((process.memoryUsage().rss/1000000 - initialRSS)*10)/10)
}
var queue = [];
for(var i=0; i<20*80; i++) {
queue.push(buf);
}
function writeNext() {
if (queue.length > 0) {
//sp.write(queue.shift(), writeNext);
sp.write(queue.shift());
writeNext();
}
else setTimeout(onFrame, 1000/60)
//else sp.drain(onFrame)
}
writeNext();
}
sp.open(onOpen);
@vorg Have you tried with the most recent (just getting back to flushign tickets out wanted to check on current status).
We have also problem with serialport's increasing memory usage. We are using long running node process that uses serialport for communication in our projects.
I tested this issue and also https://github.com/voodootikigod/node-serialport/issues/176.
My test setup is two raspberry pi's running with arch linux. Those are connected with serial connection. First one has basic echo configured and second one is running node-serialport v 1.4.9.
I ran the same test as above except 57600 port speed was used. I got same result, memory usage is increasing rapidly and after a while process stopped responding.
Runned also test case arduinoTest/stress.js from issue #176. There I changed write buffer data to get memory grow faster see changes below. There was also significant memory increase with this test.
var data = new Buffer('Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rerum, distinctio! Recusandae incidunt neque laboriosam cumque mollitia odit nulla quia ea maxime praesentium cum vitae, pariatur inventore officia ut. Ut, quae!');
//var data = new Buffer("hello");
I'm closing due to age, but we've had some major rewrites of our c++ layer, fixing a lot of memory leaks since this bug was filed. I hope it's been fixed and if that's not the case please reopen.