【发布时间】:2017-09-15 04:34:52
【问题描述】:
我正面临 “糟糕!Google chrome 在尝试显示网页时内存不足。”
下面是我每秒调用一个 API 的简单应用。每次调用后,该选项卡的 chrome 内存分配大小会不断增加。但不会减少内存。最后导致页面崩溃问题。
<!DOCTYPE html>
<html>
<body>
<div id="time">
<h1>Time Sample Timer App</h1>
<button type="button" onclick="getTime()">Start Timer</button>
</div>
<script>
function getTime() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("time").innerHTML =
JSON.parse(this.responseText).time;
setTimeout(getTime, 1000);
}
};
xhttp.open("GET", "http://date.jsontest.com", true);
xhttp.send();
}
</script>
</body>
</html>
请帮我找出问题的根本原因。
我还在Aw Snap! Google chrome ran out of memory while trying to display web page.上发布了我的观察错误
但在 Google 方面,没有人回复我的应用程序或 google chrome 本身的实际问题。
【问题讨论】:
-
这是因为您为每个 setTimeout 分配内存但没有清除它们
标签: javascript google-chrome memory-leaks xmlhttprequest out-of-memory