我正在使用XMLHttpRequest下载大小为750mb的文件作为数组缓冲区。问题在于,在下载文件之后,即使在删除XMLHttpRequest对象之后,存储器也不会释放给os。我的示例代码是
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<button type="button" onclick="loadXMLDoc()">load the file</button>
<button type="button" onclick="clear()">clear</button>
</div>
<script>
var xhttp = null;
function loadXMLDoc() {
if(xhttp == null)
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(xhttp.response);
delete xhttp;
xhttp = null;
}
};
xhttp.open("GET", "http://192.168.1.104/gltf/bmw/buffer.bin", true); //buffer.bin is 750 mb
xhttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhttp.responseType = 'arraybuffer';
xhttp.send();
}
function clear()
{
delete xhttp;
xhttp = null;
}
</script>
</body>
</html>
1)这是删除XMLHttpRequest对象的正确方法吗? 2)如何使XMLHttpRequest将内存释放回操作系统?
谢谢
发布于 2018-08-16 14:42:07
不完全同意。delete <variable name>
甚至看起来令人困惑(我很惊讶它能正常工作而不抛出错误)。总的来说,<variable> = null
是正确的方法。但在你的情况下,这应该不会有什么不同。
JS实现了自己的memory management。一旦其他人不使用相关变量,垃圾回收器就会释放内存。
问题是Garabge收集器没有连续运行。更确切地说,它是经常被称为。尝试单击开发人员工具中的“收集垃圾”按钮。这将立即调用GC。
这不是一种解决方案,而是一种调查内存是否可以释放的方法,因为从某个地方引用了您的数据。
发布于 2018-08-16 17:43:20
我发现问题出在" console.log ()".If上,我注释掉了console.log行,然后内存被释放回操作系统。
https://stackoverflow.com/questions/51870795
复制相似问题