javascript - node.js readFile memory leak -


im using node.js read image every second. idea later push web browsers, making simple stream. code looks 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 code above seem memory leak memory fills up. larger image file load, quicker fills up.

what doing wrong? there way 'release' image variable? i've tried nulling it.

this better approach. setinterval() should not used here because lead simultaneous execution of same task. use settimeout() instead. ideal place 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 line executed     }); }  starttimeout(); 

you need make sure don't mantain reference image after process it, otherwise v8's garbage collector won't free image data, leaking memory.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -