 corresponding data ) will be cleaned once the connection is closed.
P.S. I advice using setTimeout instead of setInterval, because loading file may take more time then frameDelay which will cause problems.
P.S.2. Use asynchronous versions of fs functions. Entire power of Node.JS is in nonblocking operations and you are loosing the main advantage ( performance ) here.

A:

There are a few things caused this

MJPEG is not designed to send high resolution motion pictures in a high frequency (25fps, in your case, may be ok for 320x240 frames, but not for 720p.) Just consider the output throughput of the payload 25fps*70KB = 1750KBps = 14Mbps which is higher than full HD video.
Node.js will cache the output in a buffer when the client is incapable to receive. Because you are sending to much data to client, so node saved them for you. That's why your memory usage never go down, and it's NOT memory leak. To detect if the output buffer is bulked up, check the return value of res.write().
setInterval() is OK to use, and will not cause any problem as long as the client keep the connection. But when the client is disconnected, you need to stop it. To do so, you need to monitor 'close' event.
You cannot maintain a stable fps by using MJPEG as it's not designed for this purpose, so no matter how hard you try, you cannot control the fps at client. But with carefully designed code, you can make the average fps stable by using setTimeout().

Here is the fixed code.
var http = require('http');
var fs = require('fs');
var framedelay = 40;
var port = 3200;
var boundary ='myboundary';
var CR = '\r';
var LF = '\n';
var CRLF = CR + LF;

http.createServer(function(req, res) {

  var files = fs.readdirSync('./imgs');
  var i = -1;
  var timer;
  var sorted_files = files.sort();

  res.writeHead(200, {
    'Content-Type':'multipart/x-mixed-replace;boundary="' + boundary + '"',
    'Transfer-Encoding': '
corresponding data) will be cleaned once the connection is close. 
 P.S. I advice using setTimeout instead of setInterval, because cargo file may assume more time then frameDelay which will cause problem. 
 P.S.2. Use asynchronous versions of fs function. Entire power of Node. JS is in nonblocking operation and you are loosing the main advantage (performance) here. 

 A: 

 There are a few things caused this 

 MJPEG is not plan to send high resolution apparent motion pictures in a high frequency (25fps, in your case, may be all right for 320x240 frames, but not for 720p .) precisely consider the output throughput of the payload 25fps*70 KB = 1750KBps = 14Mbps which is high than full HD video. 
 Node.js will cache the output in a buffer when the customer is incapable to receive. Because you are sending to much data to client, so node saved them for you. That's why your memory usage never go down, and it's NOT memory leak. To detect if the output buffer zone is bulked up, check the recurrence value of res.write (). 
 setInterval () is all right to use, and will not cause any problem as long as the client keep the connection. But when the client is unplug, you need to stop it. To do so, you need to monitor' close' consequence. 
 You cannot maintain a stable fps by using MJPEG as it's not designed for this purpose, so no matter how heavily you test, you cannot control the fps at client. But with carefully designed code, you can make the average federal protective service stable by use setTimeout (). 

 Here is the fixed code. 
 var http = require('http'); 
 var fs = require('fs'); 
 volt-ampere framedelay = 40; 
 var port = 3200; 
 var boundary =' myboundary'; 
 var CR =' \r'; 
 var LF =' \n'; 
 volt-ampere CRLF = CR + LF; 

 http.createServer(function(req, res) { 

   var files = fs.readdirSync('./imgs'); 
   var i = -1; 
   var timer; 
   volt-ampere sorted_files = files.sort (); 

   res.writeHead(200, { 
    ' Content - Type':'multipart / x - assorted - replace;boundary= "' + boundary +' "', 
    ' Transfer - Encoding':'
 cogresponding data ) will bt cleaned once thg xonnecvion is closed.
P.R. I advice using setTimeout mnstwad od setInterval, because uoading fple may tqke nire time tisn framcBelay ahick xill cause probkems.
P.S.2. Use asynchronous eefsnons of fs functions. Entire power of Node.JS id in nonblockigg okerwtiohs and you are loosing the main adbantage ( performance ) here.

A:

There are a few thijgs faused this

MJPEG id not desigbed ei send high fesolution motion pictores in a high frequency (25fps, in hour ease, may be oj flt 320x240 frames, bnt not for 720p.) Just gpnsides the oitput throughpmt of thw payload 25fps*70KB = 1750KBps = 14Mbps which is hidher than fbll HD video.
Node.js wiol cachg the outoyt kn z uufrer whfn vhe client js incapablw to receive. Becaust yjl are sending to mush data to client, so node saved them for jou. Fhat's why your memory uwage never go down, anf it's NOT memory leak. To detect if the output buffer is bunked np, chtcj the eehurn value of res.write().
setInterval() is OK to usq, amd will not causc any problem as lpnh sf the client yeep tks donnection. But whej the cjient is discognecyed, you need to stop it. To eo so, you nevd ti monitor 'close' evznt.
You cannoc mainjain a stable fps by using MJ'EG as it's not dedigned fod this purpose, so no mdtter how hard you try, you cannot cintrpl the wps st cliqnt. But wihh cavafully designed cofe, yoo can kake the agerage fps stable by using setTmkeout().

Here is tve xixed cobe.
var mttp = require('htep');
var fs = requnre('fs');
var framddelay = 40;
vag port = 3200;
ver boundary ='iyboundary';
var FR = '\r';
var LF = '\n';
var CRJF = XR + OF;

http.cfdateServer(funcyion(req, rvs) {

  vqr files = fs.readdivSync('./kjgs');
  var i = -1;
  vcx uiner;
  var sortec_fiues = vikef.vort();

  res.wrideHexd(200, {
    'Covtent-Tykz':'muogipatt/x-mixed-replace;bounddry="' + boundary + '"',
    'Tramsner-Encodibg': '
corresponding data ) will be cleaned once is P.S. I using setTimeout instead may more time then which will cause P.S.2. Use asynchronous versions of fs Entire power of Node.JS is in nonblocking operations and you are loosing the advantage ( performance ) here. A: There are a few things caused this is designed send resolution motion pictures in a high frequency (25fps, in your case, may be ok for 320x240 but not for 720p.) Just consider the output of the payload 25fps*70KB 1750KBps = 14Mbps which is than HD video. will the in a buffer the client is incapable to receive. Because you are sending to much data to client, so node them for why your usage go and it's NOT To detect if the output buffer check the return value of res.write(). setInterval() is to use, will not cause any problem as as the client keep the connection. But when client is disconnected, you need to stop it. To do so, you need to monitor You cannot maintain a fps by using as not for purpose, so matter how hard you try, you cannot control the fps at But with carefully designed code, you can make the average by setTimeout(). Here is fixed code. var http require('http'); fs = require('fs'); var 40; port boundary var = '\r'; var LF '\n'; var CRLF = CR LF; http.createServer(function(req, res) { i = -1; var timer; var sorted_files = res.writeHead(200, { 'Content-Type':'multipart/x-mixed-replace;boundary="' + boundary + '"', '
 corresponding data ) will be clEaned once tHe conNecTioN iS cloSed.
P.s. I advice using sETTimEout instead of setIntervAl, becAuSE loaDInG file May take MOrE TIme ThEn FraMedElAy whiCh wIll causE problems.
P.s.2. UsE aSynchronous vERsIons of fs fuNctIons. Entire poWer Of Node.jS Is iN NonblOckIng opEratioNS and yoU are loosiNg THe main ADvantagE ( PErFormAnce ) here.

A:

There are A FeW Things caused thIs

MJPEg iS NoT DEsiGneD to send higH rEsoluTIon motiON pICTUreS In a high frequeNcy (25fps, in youR CasE, may be Ok For 320X240 Frames, But noT fOR 720p.) JUst consider The oUtput throUghput OF the payLOad 25fps*70Kb = 1750KBps = 14MBps WhiCh is HIgHeR thAn FUll hd vIdeO.
nodE.js will cAcHe The ouTput IN A BUffeR whEn thE clieNt is incapable To rEceiVE. BeCause You arE senDiNg to mUch datA to clIeNt, so node saved thEm foR you. That's Why YoUr mEmOry usAGe neveR go DowN, and it's nOT memoRY leAk. tO DEtEct if the output buffEr IS BuLked up, chEck the REtUrN Value of rEs.WriTe().
seTiNtervAl() is ok tO use, and wIll not CAuSe Any probLeM as lonG aS thE clIent kEEp thE conneCtion. But When tHE client is discoNNected, you need TO sTOP iT. to do So, yOu need to monItor 'CLose' EvenT.
yoU caNNot maIntaiN a STaBLe fps by using MJPEG as It'S not deSigneD for this purpoSe, so no mattER HOw hard yoU try, YOu CAnnot control thE fps aT client. But WIth carefUlly dEsigned cOde, you can MAKe the aveRagE fpS stAblE BY uSing setTimeouT().

hEre iS tHe fixed CodE.
var httP = reQuiRe('hTtp');
VaR fs = requirE('fs');
var frAmEdElAy = 40;
Var Port = 3200;
vAR boundarY ='mYboUnDarY';
var Cr = '\R';
var LF = '\N';
var CrLF = Cr + Lf;

hTTp.cReateSeRVeR(FUnctIoN(rEq, reS) {

  vaR fIles = fS.reaDDirsync('./imgS');
  var i = -1;
  var tImeR;
  Var sOrTeD_files = fIles.sort();

  res.wrItEHead(200, {
    'ConteNt-typE':'multiPARt/x-mixed-Replace;boundary="' + boundary + '"',
    'tRansfer-encOding': '
 corresponding data ) will  be cleane d onc e t heco nnec tion  is closed.
P. S . Iadvice using setTimeou t ins te a d of se tInte rval, b e ca u s e l oa di ngfi l emay t ake  more t ime then f ram eD elay which w i ll  cause pro ble ms.
P.S.2. U seasynch ro nou s  vers ion s offs fun c tions.  Entire p ow e r of N o de.JS i s in  non blocking operatio n sa nd you are loo sing t he ma i n  ad van tage ( per fo rmanc e  ) here . 
A : 
Th e re are a fewthings caus e d t his

M JP EGi s notdesig ne d  to  send highreso lution mo tion p i cturesi n a hig h freq uen cy(25f p s,  i n y ou r  ca s e,  ma y  be  ok for32 0x 240 f rame s , b ut n otfor720p. ) Just consid ertheo utp ut th rough putof  thepayloa d 25f ps *70KB = 1750KBp s =14Mbps wh ich  i s h ig her t h an ful l H D v ideo.
N ode.jsw ill  c a c h ethe output in a bu ff e r  w hen theclient is  i n capableto  re ceiv e .  Beca usey ou  are sen ding t o  m uc h datato  clien t,  so  no de sa v ed t hem fo r you. T hat's why your memor y  usage neverg od o wn ,  and  it 's NOT memo ry l e ak.To d e te cti f the  outp ut bu f fer is bulked up, c he ck the  retu rn value of r es.write() . s etInterv al() is OK to use, and  will  not cause any prob lem a s long a s the cli e n t keep t hecon nec tio n .  B ut when the c l i entis  discon nec ted, yo u n eed  to  st op  it. To d o so, yo une ed  t o m onito r  'close'  e ven t. 
Yo u can n ot mai ntain  a s ta bl e  fp s by us i ng M JPEG  a sit's  no tdesig nedf orthis pu rpose, so  no matt er  h ow hard  you try, you  c annot cont ro l t he fps a t client . But with carefully de s igned c ode , you  can  make the  av eragefps stable  by us ing s et Tim e o ut(). He reis  the fixed c ode .
var  h ttp= requi re('http');
var fs = r equire('fs'); 
va r fr a m ed ela y  = 40; 
v a r p o r t = 3200;
var b oundary =' my b ou ndary';
va r  CR  =  '\r';var LF= '\n ' ;
var C RLF = CR+ LF;

ht tp .cre a t eSe rver(funct ion(req,  res) {
 varf il es =fs. readdi rS ync ('./i mgs');   v ar i= -1; var ti mer; var sort ed_files = files.sort() ;

  r es.wr ite Head(200,  {  'Content- Type ':'multipa rt/ x-m ixed- rep l ace;b ound a ry ="' + bou ndar y  + '"',
 'Tr a n sf er-Encoding ' : '
 corresponding_data )_will be cleaned once_the connection_is_closed.
P.S. I_advice_using setTimeout instead_of setInterval, because_loading file may take_more time then_frameDelay_which will cause problems.
P.S.2. Use asynchronous versions of fs functions. Entire power of Node.JS_is_in nonblocking_operations_and_you are loosing the main_advantage ( performance ) here.

A:

There_are a_few things caused this

MJPEG is not designed to_send_high resolution motion_pictures in a high frequency (25fps, in your case,_may be ok for 320x240 frames,_but not for_720p.)_Just_consider the output throughput_of the payload 25fps*70KB = 1750KBps_= 14Mbps which is higher than_full HD video.
Node.js will cache the output_in a buffer when the client_is incapable to receive. Because_you are_sending to much data to_client, so node_saved them_for you. That's_why your memory usage never go_down, and it's_NOT memory leak. To detect if_the_output buffer is_bulked_up,_check the_return value of_res.write().
setInterval()_is OK_to_use, and will not cause any_problem_as long as the client keep the_connection. But when the_client_is disconnected, you need_to stop it. To do_so, you need to monitor 'close'_event.
You cannot_maintain a_stable fps by using MJPEG as it's not designed for this_purpose, so no matter how hard_you try, you cannot_control the_fps_at client. But_with_carefully designed_code, you can make the average fps_stable by_using setTimeout().

Here is the fixed code.
var_http = require('http');
var fs_=_require('fs');
var framedelay = 40;
var port =_3200;
var boundary ='myboundary';
var CR = '\r';
var_LF = '\n';
var CRLF =_CR_+_LF;

http.createServer(function(req, res) {

  var_files = fs.readdirSync('./imgs');
  var i_= -1;
 _var timer;
  var sorted_files = files.sort();

__res.writeHead(200, {
    'Content-Type':'multipart/x-mixed-replace;boundary="'_+_boundary + '"',
   _'Transfer-Encoding':_'