简体   繁体   中英

Node.js memory leak when using .map to create an array

I'm making repeated requests to a website using node, and trying to debug a resulting memory leak. From using node --inspect server.js I have been able to guess that this is the code causing the problem (this is a section of a Promise, hence the "resolve()" at the end):

response.on('end', function () {
    //store response in variable
    bufferData = Buffer.concat(bufferData);
    try {
        var bufferDataDecoded  = thisProtocol.FeedMessage.decode(bufferData).entity;
    } catch (e) {
        console.log("Protocol Error:  "+e);
        resolve([0]);
    }
    var timeNow = new Date();
    var bufferDataMapped= bufferDataDecoded
    .map(obj=> {
        return obj.item_update
            .map(itemu=>{
            if ((itemu.quantityOld-itemu.quantityNew) > 0 ){
                return {
                "resultType": bufferType
                ,"itemID": itemu.itemID
                ,"quantityOld": itemu.quantityOld
                ,"quantityDiff": itemu.quantityNew - itemu.quantityOld
                };
            }
            else return false;
        })
    });
    var bufferDataMapped2= [].concat.apply([],bufferDataMapped);
    resolve(bufferDataMapped2);
}); 

From looking at the Comparison log coming out of --inspect and looked at with Chrome DevTools, it looks like node.js is saving these objects like:

 {
   "resultType": bufferType
   ,"itemID": itemu.itemID
   ,"quantityOld": itemu.quantityOld
   ,"quantityDiff": itemu.quantityNew - itemu.quantityNew
 };

...every time the program runs, and not deleting the old ones after it runs, which is quickly contributing to a memory overflow. I created variables locally within the .map() in an attempt to avoid this problem, and am not sure what I'm doing wrong. Any ideas why this would be happening?

I am missing some code, so, here is my conclusion. Take a look into this code:

bufferData = Buffer.concat(bufferData);

bufferData is a global variable, so in each execution is concatenating the previous executions data. Try assigning null in the end of each execution:

response.on('end', function () {
    ... rest of the code ...

    resolve(bufferDataMapped2);
    bufferData = null;
}); 

But, to have a better response I would like to see the rest of the code.

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