javascript - SBOX_FATAL_MEMORY_EXCEEDED when uploading a file in chunks
问题描述
I have this (simplified) code to upload a file as chunk :
let reader = new FileReader();
let blob = file.slice(0, STEP);
reader.readAsDataURL(blob);
reader.onload = (e) =>
{
let d =
{
container: container,
blob: BlobName,
file: reader.result,
id: id
};
$.ajax({
url: uploadPath,
type: "POST",
data: d,
timeout: 30000
}).done(function(r)
{
if (r.success == "yes")
{
Loaded += e.loaded;
if(Loaded < total)
{
blob = file.slice(Loaded, Loaded + STEP); // getting next chunk
reader.readAsDataURL(blob); // trigger onload for next chunk
}
else
{
// File is completely uploaded
}
}
else
{
if (tries++ > 3)
{
// error management here
}
else
{
// try again
reader.readAsDataURL(blob); // trigger again onload
}
}
}).fail(function (jqXHR, textStatus, errorThrown)
{
if (tries++ > 3)
{
// error management here
}
else
{
// try again
reader.readAsDataURL(blob); // trigger again onload
}
}
}
This code worked like a charm, even for large files (43 GB).
Today, we had to upload a large file (20 GB) and we got a SBOX_FATAL_MEMORY_EXCEEDED
on Chrome (88)
After lots of tests and monitoring, we noticed a HUGE memory usage growing up in Chrome when using this upload.
Other tests were made and we noticed the same behavior on Edge and Firefox (upload could be done on FF, but still used GBs of RAM)
What can I do to fix this terrible memory management?
解决方案
似乎事件的递归触发器阻止了块立即被 GC
可以将块的引用设置为null
使它们符合 GC 条件:
在每个之前readAsDataURL()
,添加以下内容:
reader.result = null; // the result itself
d.file = null; // the chunk in the current object sent to the server
reader.readAsDataURL(blob);
这现在可以在正确管理内存的情况下正常工作,在上传期间保持稳定
推荐阅读
- r - 在r中循环两个代码和两个函数
- linux - bash echo 输出字符串和字符的混合
- javascript - CSS:按下与单选按钮分组的下拉菜单
- c# - Unity ScreenToWorldPoint 在相机后面返回
- c++ - C ++:由于“temp [0] [0] = array [0] [0]”不起作用,如何将二维动态数组中的值分配给另一个二维动态数组?
- java - 如何让等待 LOAD DATA LOCAL INFILE 完成 - Java
- spring - Spring kafka没有收到消息
- javascript - 在画布中重塑圆圈
- c++ - 传递一个带有N个参数的模板,作为模板参数,用N-1个参数指定
- javascript - 自定义标题不占用堆栈栏中的全部空间