FileReader memory leak in Chrome

徘徊边缘 提交于 2021-02-07 03:49:40

问题


I have a webpage with file upload functionality. The upload is performed in 5MB chunks. I want to calculate hash for each chunk before sending it to the server. The chunks are represented by Blob objects. In order to calculate the hash I am reading such blob into an ArrayBuffer using a native FileReader. Here is the code:

var reader = new FileReader();

var getHash = function (blob, callback) {
    reader.onloadend = function (e) {
        var hash = util.hash(e.target.result);
        callback(hash);
    };
    reader.readAsArrayBuffer(blob);
}

var processChunk = function (chunk) {
    if (chunk) {
        getHash(chunk, function (hash) {
            util.sendToServer(chunk, hash, function() {
                // this callback is called when chunk upload is finished
                processChunk(chunks.shift());
            });
        });
    }
}

var chunks = file.splitIntoChunks(); // gets an array of blobs
processChunk(chunks.shift());

The problem: using the FileReader.readAsArrayBuffer seems to eat up a lot of memory which is not released. So far I tested with a 5GB file on following browsers:

  • Chrome 55.0.2883.87 m (64-bit): the memory goes up to 1-2GB quickly and oscillates around that. Sometimes it goes all the way up and browser tab crashes. It can use more memory than the size of read chunks. E.g. after reading 500MB of chunks the process already uses 700MB of memory.

  • Firefox 50.1.0: memory usage oscillates around 300-600MB

Code adjustments I have tried - all to no avail:

  • re-using the same FileReader instance for all chunks (as suggested in this question)
  • creating new FileReader for each chunk
  • adding timeout before starting new chunk
  • setting the FileReader and the ArrayBuffer to null after each read

The question: is there a way to fix the problem? Is this a bug in the FileReader implementations or am I doing something wrong?

EDIT: Here is a JSFiddle https://jsfiddle.net/andy250/pjt9udeu/


回答1:


This is a bug in Chrome on Windows. It is reported here: https://bugs.chromium.org/p/chromium/issues/detail?id=674903



来源:https://stackoverflow.com/questions/41181923/filereader-memory-leak-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!