页面跳转时,为什么XHR会发生内存泄漏?

时间:2019-05-14 12:34:13

标签: javascript

浏览器:IE 11.0.9600.19326

当页面上有长链接XHR请求时,单击#导航时会发生内存泄漏。可以排除由于计时器引起的泄漏,因为在跳转#之前不会发生泄漏。

enter image description here

我的代码:

var isGetDataBusying = false;

function GetData() {
  if (isGetDataBusying == true) {
    return;
  } else {
    isGetDataBusying = true;
  }
  var xhr = null;
  xhr = new XMLHttpRequest();
  xhr.open("POST", "/test.txt", true);
  xhr.timeout = 1000;
  xhr.onload = success;
  xhr.onloadend = end;
  xhr.send(null);

  function success() {
    if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {} else {}
  }

  function end() {
    xhr = null;
    isGetDataBusying = false;
  }
}

setInterval(GetData, 2);
<html>

<head>
  <title>XHR Memory Leak</title>
</head>

<a href="#">#</a>

</html>

需要一台高性能计算机来确保测试,因为我发现在某些性能较低的计算机上,即使不单击#链接,它也会泄漏。但是单击#链接后,泄漏的内存与内容不同。

0 个答案:

没有答案