问题是即使上传完成或关闭图像页面后,内存仍然没有被释放,Firefox仍然保持着2GB的内存占用。 是否可以从JavaScript中以某种方式释放内存,例如在上传完成后或图像加载完成/关闭后?
编辑
从about:memory
:
1,172.03 MB(100.0%)-- 明确
├──1,000.00 MB(85.32%)-- js
│ ├────863.97 MB(73.72%)-- compartment([System Principal], 0x5083000)
│ │ ├──819.31 MB(69.91%)── string-chars
如何清空string-chars?我非常确定这是在将文件读入内存并使用ajax上传时出现的。
编辑2
以下是导致该内存使用情况的递归函数:
function uploadAjax(file, startByte, index)
{
if(startByte==0)
{
$('#progress'+index).html(' ').progressbar( "destroy" ).progressbar();
$('#asyncuploadsingle'+index).attr('disabled', true);
}
var size = file.size;
var chunkSize = 2097152;//2 megabyte
var endByte = chunkSize + startByte;
var isLast = (size - endByte <= 0);
var chunk = file;
var xhr = new XMLHttpRequest();//prepare xhr for upload
var chunkNum = endByte / chunkSize;
if(chunkSize == 0)//no divide
{
chunk = file;
isLast = true;
}
else if(file.mozSlice) // moz slice
{
chunk = file.mozSlice(startByte, endByte);
}
else if(file.webkitSlice) //webkit slice
{
chunk = file.webkitSlice(startByte, endByte);
}
else if(file.slice) // w3c slice
{
chunk = file.slice(startByte, chunkSize);
}
else
{
chunk = file;
isLast = true;
}
//progress function, with ajax upload progress can be monitored
xhr.upload.addEventListener('progress', function(e)
{
if (e.lengthComputable)
{
var perc = Math.round((e.loaded + chunkNum * chunkSize - chunkSize) * 100 / size);
//console.log(perc+':'+index);
$('#progress'+index).progressbar("option", "value", perc);
}
}, false);
xhr.upload.onabort=function(e) {
finishUp(index,'Aborted');
};
xhr.upload.addEventListener('error', function(e){
finishUp(index, this.responseText+'--->'+name);
}, false);
xhr.onreadystatechange=function()
{
if(this.readyState == 4 && this.status == 200)
{
try
{
var ret = JSON.parse(this.responseText);
if(isLast)
{
finishUp(index,'');
}
else if(ret.status == 'error')
{
throw ret.info;
}
else
{
uploadAjax(file, endByte, index);
}
}
catch(err)
{
finishUp(index, err);
}
delete chunk;
}
};
var path = get_final_path();
var url = "filetransfer/uploadfiles.php?ax-file-name="+encodeURIComponent(file.name)+"&ax-file-path="+encodeURIComponent(path)+'&ax-start-byte='+startByte;
xhr.open("POST", url, true);
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');//header
xhr.setRequestHeader('Content-Type', 'application/octet-stream');//generic stream header
/*var reader = new FileReader();
reader.onload = function(evt) {
xhr.sendAsBinary(evt.target.result);
};
reader.readAsBinaryString(chunk);
*/
xhr.send(chunk);
return xhr;
}
有哪些地方可以进行优化或释放对象?
about:memory
。 - Rob W