简体   繁体   中英

node.js readFile memory leak

Im using node.js to read an image every second. The idea is to later push it to web browsers, making it a simple stream. The code looks as following:

var fs      = require("fs");
var intervalTimerObj;

function startStream() {
    if(!intervalTimerObj) {
        intervalTimerObj = setInterval( updateStream  , 1000);
    }
}

function updateStream(){
    fs.readFile( __dirname + "/pic.jpg", function(err, image) {

    });
}

startStream();

When running the code above I seem to get a memory leak as my memory quickly fills up. The larger the image file i load, the quicker it fills up.

What am I doing wrong? Is there some way to 'release' the image variable? I've tried nulling it.

This is a better approach. setInterval() should not be used here because it could lead to simultaneous execution of the same task. Use setTimeout() instead. The ideal place to do it is inside readFile() 's callback:

var fs            = require("fs");
var timeoutHandle = null;

function startTimeout() {
  stopTimeout();
  timeoutHandle = setTimeout(updateStream, 1000);
}

function stopTimeout() {
  clearTimeout(timeoutHandle);
}

function updateStream(){
    fs.readFile( __dirname + "/pic.jpg", function(err, image) {
      // ...

      startTimeout(); // Make sure this line is always executed
    });
}

startTimeout();

You also need to make sure you don't mantain any reference to image after you process it, otherwise V8's garbage collector won't free the image data, thus leaking memory.

Try this

function startStream() {
    if(!intervalTimerObj) {
        intervalTimerObj = setTimeout( updateStream  , 1000);
    }
}

function updateStream(){
    fs.readFile( __dirname + "/pic.jpg", function(err, image) {
       //OK file is read
       //do yoyr stuff with it....
       // Now start next read
       startStream()
    });
}

startStream();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM