SBOX_FATAL_MEMORY_EXCEEDED 分块上传文件时

SBOX_FATAL_MEMORY_EXCEEDED when uploading a file in chunks

我有这个(简化的)代码可以将文件作为块上传:

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
        }
    }
}

即使对于大文件 (43 GB),此代码也非常有效。

今天,我们不得不上传一个大文件 (20 GB),我们在 Chrome (88)

上得到了一个 SBOX_FATAL_MEMORY_EXCEEDED

经过大量测试和监控,我们注意到在使用此上传时 Chrome 内存使用量增加。

进行了其他测试,我们注意到 Edge 和 Firefox 上的行为相同(上传可以在 FF 上完成,但仍然使用 GB 的 RAM)

我能做些什么来修复这个糟糕的内存管理?

事件的递归触发器似乎阻止了块立即被 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);

这现在可以通过正确的内存管理正常工作,在上传过程中保持稳定