【问题标题】:SBOX_FATAL_MEMORY_EXCEEDED when uploading a file in chunks分块上传文件时的 SBOX_FATAL_MEMORY_EXCEEDED
【发布时间】:2021-02-05 15:16:41
【问题描述】:

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

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)

我能做些什么来解决这个糟糕的内存管理问题?

【问题讨论】:

    标签: javascript file-upload chunks


    【解决方案1】:

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

    这现在可以在正确管理内存的情况下正常工作,在上传期间保持稳定

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 2017-01-08
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2021-08-14
      • 2023-03-15
      相关资源
      最近更新 更多