在FileReader之后是否可以清理内存? [英] Is it possible to clean memory after FileReader?

查看:753
本文介绍了在FileReader之后是否可以清理内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileReader似乎消耗所有的内存,因为它被重复用于预加载多个blob,并且永远不会释放它。任何已知的方法来强制它释放消耗的内存?设置FileReader对象并将其结果属性设置为null似乎不起作用。

FileReader seems to consume all the memory as it is repeatedly used to preload multiple blobs, and never frees it. Any known way to force it to release consumed memory? Setting FileReader object and it's result property to null doesn't seem to work.

更新:

这是一个示例代码(在大文件,如电影或你不会注意到任务管理器的效果):

Here is a sample code (test it on a big files, like movie, or you won't notice the effect in task manager):

<input id="file" type="file" onchange="sliceMe()" />

<script>
function sliceMe() {
    var file = document.getElementById('file').files[0], 
        fr,
        chunkSize = 2097152, 
        chunks = Math.ceil(file.size / chunkSize), 
        chunk = 0;

    function loadNext() {
       var start, end,
           blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice;

       start = chunk * chunkSize;
       end = start + chunkSize >= file.size ? file.size : start + chunkSize;

       fr = new FileReader;
       fr.onload = function() {      
          if (++chunk < chunks) {
             // shortcut - in production upload happens and then loadNext() is called
             loadNext(); 
          }
       };
       fr.readAsBinaryString(blobSlice.call(file, start, end));
    }

    loadNext();
}
</script>

我每次尝试创建新的FileReader实例,但问题仍然存在。我怀疑它可能是由模式的循环本质引起的,但我不知道在这种情况下可以使用什么其他模式。

I tried to create fresh FileReader instance every time, but the problem still stays. I suspect that it could be caused by a circular nature of the pattern, but I'm not sure what other pattern can be used in this case.

我检查了这段代码在Firefox和Chrome中,Chrome似乎更优雅地处理它 - 它在每个周期之后清除内存,速度非常快。但是,讽刺的是,Chrome不需要使用这个代码。这只是一个实验来克服Gecko 6- FormData + Blob错误( Bug 649150 - Blob没有一个文件名,如果通过FormData发送的话)。

I checked this code in both Firefox and Chrome and Chrome seems to handle it more gracefully - it purges memory after each cycle and is very fast. But the irony of the situation is that Chrome doesn't need to use this code at all. It's just an experiment to overcome Gecko 6- FormData + Blob bug (Bug 649150 - Blobs do not have a filename if sent via FormData).

推荐答案

Bug已被标记为INVALID,因为事实证明我实际上并不是正确地重新使用FileReader对象。

Bug has been marked as INVALID, since it turned out that I wasn't in fact re-using FileReader object properly.

这是一种不存储内存和cpu的模式:

Here is a pattern, which doesn't hog memory and cpu:

function sliceMe() {
    var file = document.getElementById('file').files[0],
        fr = new FileReader,
        chunkSize = 2097152,
        chunks = Math.ceil(file.size / chunkSize),
        chunk = 0;

    function loadNext() {
       var start, end,
           blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice;

       start = chunk * chunkSize;
       end = start + chunkSize >= file.size ? file.size : start + chunkSize;

       fr.onload = function() {      
          if (++chunk < chunks) {
             //console.info(chunk);
             loadNext(); // shortcut here
          }
       };
       fr.readAsBinaryString(blobSlice.call(file, start, end));
    }

    loadNext();
}

另一个错误报告已提交: https://bugzilla.mozilla.org/show_bug.cgi?id=681479 ,这是相关的,但不是邪恶的在这种情况下。

Another bug report has been filed: https://bugzilla.mozilla.org/show_bug.cgi?id=681479, which is related, but not the evil in this case.

感谢Kyle Huey提请我注意:)

Thanks to Kyle Huey for bringing this to my attention :)

这篇关于在FileReader之后是否可以清理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆