我注意到在使用 firefox 几个小时后,它获得了超过 2gb 的 ram 内存。当我使用 ajax 上传器上传大文件(100mb-400mb)时会发生这种情况,当我打开许多图像时也会发生这种情况(例如网页上的总图像数为 16mb)。
问题是,即使在上传完成或关闭图像页面后,内存也没有得到释放,firefox 仍然有 2gb 的 ram 内存。javascript有没有办法让firefox释放内存,例如上传完成或加载或关闭图像后?
编辑
自about:memory
:1,172.03 MB (100.0%) -- 显式
├──1,000.00 MB (85.32%) -- js
│ ├────863.97 MB (73.72%) -- 隔间([System Principal], 0x5083000)
│ │ ├──819.31 MB (69.91%) ── 字符串字符
当文件被读入内存然后用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;
}
在哪里优化它或在哪里释放对象?