TooTallNate / node-speaker

Output PCM audio data to the speakers
655 stars 147 forks source link

Memory Leak #155

Closed boedy closed 4 years ago

boedy commented 4 years ago

I'm experiencing a memory leak on my raspberry PI using node-speaker. Running the code copied from @LinusU in this PR https://github.com/TooTallNate/node-speaker/pull/22) will slowly consume all available memory:

var Speaker = require('speaker');
var RandStream = require('randstream');

var s = new Speaker();
var firehose = new RandStream();
firehose.pipe(s);

I'm not able to pinpoint the error, but based on my heap profiles a lot of object are remaining behind, which are not being cleaned by the GC. See a screenshot of that below:

Screenshot 2020-10-30 at 17 39 28

Tested on: Raspberry PI 3B+ NodeJS (v8.11.4)

boedy commented 4 years ago

Turns out this is a bug in the node-addon-api for node versions prior to v8.12. See https://github.com/nodejs/node-addon-api/issues/237. For people affected this issue it's recommended you upgrade.

This line was causing the issue: https://github.com/TooTallNate/node-speaker/blob/master/src/binding.c#L122

As for other memory leaks. https://github.com/TooTallNate/node-speaker/pull/144 still needs to be merged as it is causing leaks for all node versions.