nodejs memoryleak in on end listener in express

75 views
Skip to first unread message

karim elsafy

unread,
Feb 12, 2015, 8:07:42 AM2/12/15
to nod...@googlegroups.com, Mahitab Farid, Bassam Khallaf
hi,
I have a problem on my code, that some variables never deleted/released. I tested it using heapdump. for example:
    
    var app = express();
    app.post("/myURL", function(req, res){
         var reqBody = new Buffer(0);
         req.on('data', function (data) {
               data = new Buffer(data);
               reqBody = Buffer.concat([reqBody,data]);
         });
      
         req.on('end', function () {
              // use the reqBody and
              // in some callbacks do the following line
              res.send("OK");
         });
    )};

here I found that variable reqBody never released (with some variables in on end method) and the memory is grown for large data.

Phillip Johnsen

unread,
Feb 13, 2015, 3:12:11 AM2/13/15
to nod...@googlegroups.com, engm...@gmail.com, eng.bassam...@gmail.com
Hi Karim,

have you tried req.once('end', ...)?


- Phillip

Stefan Thies

unread,
Feb 14, 2015, 4:19:13 AM2/14/15
to nod...@googlegroups.com, engm...@gmail.com, eng.bassam...@gmail.com
Hi Karim,

a) Is there any reason you don't use  req.body ? See Express documentation.
b) The right way would be to attach the buffer to the request object , e.g. when receiving first data event
     instead of having it living in the scope of your functions (which remains all the time and might be the reason teh garbage collector can't remove it).
     This is what Express does with the req.body
    
BR
Stefan

karim elsafy

unread,
Feb 15, 2015, 4:24:46 AM2/15/15
to nod...@googlegroups.com, engm...@gmail.com, eng.bassam...@gmail.com
Hi Guys,

@Phillip: I tried it now and it didn't work also.

@Stefan: this is because the data are sent in Chunks not one chunk, so I dont think req.body would work in this case.

The workaround I do now is that I null the variables after I finish using them,
Reply all
Reply to author
Forward
0 new messages