0

All:

I am just start learning JS memory leaking, if I write a node module like(what I want to do is making blocking ajax call one after one in order, and eventually fill that bigtable):

var axios = require("axios")
var bigtable = []
var somedataURL = "some data URL here"
function buildReq(dataURL){
    return axios.get(dataURL)
                .then(function(response) {
                    // response.data is just a simple array
                    var data = response.data.slice(0);
                    bigtable = bigtable.concat(data);
                    return bigtable;
                })
}
var req = buildURL(somedataURL);
for(var i = 0; i<10; i++){
    req = req.then(function(bigtable){
        return buildReq(somedataURL_IdontCARE);
    })
}
req.then(function(bigtable){
    console.log("final:"+bigtable)
})
module.exports = bigtable

For this specific example, could anyone help to point out if there is memory leak? Why?

Thanks

Kuan
  • 11,149
  • 23
  • 93
  • 201
  • No but there is a very ugly modification of a global state. `bigtable` should not exist at all. – Jonas Wilms Apr 19 '18 at 17:52
  • @JonasW. Thanks, BTW, could you give some pattern that could lead to memory leak in similar use case scenario? Right now, I just have big difficulty to identify them – Kuan Apr 19 '18 at 18:00
  • I would say `bigtable` is the memory leak :-) – Bergi Apr 19 '18 at 18:44
  • [This answer](https://stackoverflow.com/a/29931657/1048572) also discusses your case. Memory management isn't optimal, but it will not matter for 10 chained promises. And as @JonasW. already said, no the promises do not leak memory – Bergi Apr 19 '18 at 18:46
  • You a) should export the promise instead of the array, as `bigtable` is unusable otherwise b) install a `catch` handler at the end of the chain to deal with errors – Bergi Apr 19 '18 at 18:47
  • @Bergi Thanks. Could you give a lil bit more detail about how that bigtable becomes memory leak? I also suspect that, but I just can not understand reason? In my actual example, there is more than 100 chained together. So I am a lil worried. My main issue is to recognize which is referenced which is not... For example, that bigtable is more like a global variable inside this module, will that make each scope of resolve function stayed in memory? – Kuan Apr 19 '18 at 21:03
  • @Kuan `bigtable`, being the export value of your module, is staying in memory until your app shuts down - like a static, global variable indeed. It might or might not be the expected behaviour to forever keep around all the data that you put into it. – Bergi Apr 19 '18 at 21:15
  • @Kuan no, `bigtable` does not reference your promises or resolve functions, it doesn't affect their garbage collection at all. In the other direction, yes the promises do reference `bigtable` and keep it alive until the promises are resolved and the handlers are run - but that doesn't matter because `bigtable` stays up forever anyway. – Bergi Apr 19 '18 at 21:17

0 Answers0