How synchronous AJAX call could cause memory leak?

后端 未结 5 1091
野的像风
野的像风 2021-01-31 03:21

I understand this general advice given against the use of synchronous ajax calls, because the synchronous calls block the UI rendering.

The other reason generally given

5条回答
  •  鱼传尺愫
    2021-01-31 03:56

    Memory leaks using syncronous AJAX requests are often caused by:

    • using setInterval/setTimout causing circular calls.
    • XmlHttpRequest - when the reference is removed, so xhr becomes inaccessible

    Memory leak happens when the browser for some reason doesn’t release memory from objects which are not needed any more.

    This may happen because of browser bugs, browser extensions problems and, much more rarely, our mistakes in the code architecture.

    Here's an example of a memory leak being cause when running setInterval in a new context:

    var
    Context  = process.binding('evals').Context,
    Script   = process.binding('evals').Script,
    total    = 5000,
    result   = null;
    
    process.nextTick(function memory() {
      var mem = process.memoryUsage();
      console.log('rss:', Math.round(((mem.rss/1024)/1024)) + "MB");
      setTimeout(memory, 100);
    });
    
    console.log("STARTING");
    process.nextTick(function run() {
      var context = new Context();
    
      context.setInterval = setInterval;
    
      Script.runInContext('setInterval(function() {}, 0);',
                          context, 'test.js');
      total--;
      if (total) {
        process.nextTick(run);
      } else {
        console.log("COMPLETE");
      }
    });
    

提交回复
热议问题