删除Javascript blob? [英] Delete Javascript blobs?

查看:407
本文介绍了删除Javascript blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙着摆脱这些愚蠢的事情。我有几个Chrome应用程序处理大量媒体文件;其中一个我能够使用一堆删除和一个 window.URL.revokeObjectURL 最终阻止它们在 chrome中构建:// blob-internals / ,但另一个,似乎没有任何帮助。我错过了什么吗?我完全知道该死的什么时候,但我似乎无能为力。

I'm having a heck of a time getting rid of these stupid things. I've got a couple of Chrome apps that deal with lots of media files; one of them I was able to use a bunch of "delete"s and a window.URL.revokeObjectURL that finally stopped them from building up in chrome://blob-internals/, but this other one, nothing seems to help. Am I missing something? I know exactly when I'm done with the damn thing, but there seems to be nothing I can do.

具体来说,我在一个块中使用了一个File对象像这样:

Specifically, I'm using a File object in a block like this:

ref.file(function(f) {
    // Do some stuff...
    // and now I'm done!
    delete f
});

以下是我的应用的实际来源:

Here's the actual source of my app:

https://github.com/pkulak/photo-importer

这是我认为我实际解决了问题的那个,但是谁知道:

and here's the one where I think I actually solved the problem, but who really knows:

https://github.com/pkulak/drive-slideshow

推荐答案

这看起来有内存泄漏。

JavaScript 没有删除你正在谈论的感觉,垃圾收集作为属性和变量变成孤儿。 delete operator是实现这一目标的一种方式 - 它从 Object 中删除属性的定义。

使用 delete 正确意味着在属性上使用它,而不是 var iable。它对某些变量起作用的原因是因为全局命名空间中的 var 会发生什么(即它们成为窗口的属性)。这也意味着你不能删除参数。

JavaScript doesn't have a "delete" in the sense you're talking about, it garbage collects as properties and variables become orphaned. The delete operator is one such way to achieve that - it removes the definition of a property from an Object.
Using delete correctly means using it on a property, not a variable. The reason it works on some variables is because of what happens with var in the global namespace (i.e. they become properties of window). This also means you can't delete a parameter.

此外,请注意,一旦函数完成调用,如果没有引用保持活动,那么它的所有内部都将是GC。

Further, note that once a function has finished invoking, if there are no references being kept alive then all of it's internals will be GC'd.

接下来,考虑

var o = {};
o.a = [];
o.b = o.a;
delete o.a;

什么是 ob 现在?

`o.b; // []`

即使我们仍然指向数组删除了 oa 参考。这意味着数组 不会被垃圾收集。

It's still pointing at the Array even though we deleted the o.a reference. This means that the Array won't be garbage collected.

那么这对你意味着什么?

So what does this mean for you?

要摆脱 Blob ,你需要销毁对它们的所有引用。

To get rid of your Blobs, you need to destroy all the references to them.

是的,撤消 URI 是其中的一部分,但您还需要在代码中一直删除引用。如果您发现这很困难,我建议您将所有 Blob 包起来,以便至少可以最大限度地减少问题。

Yes, revoking the URI is part of it, but you also need to remove references all the way through your code. If you're finding this difficult, I'd suggest you wrap all your Blobs so you can at least minimise the problem.

var myBlob = (function () {
    var key, o;
    function myBlob(blob) {
        var url;
        this.blob = blob;
        blob = null;
        this.getURL = function () {
            if (url) return url;
            return url = URL.createObjectURL(this.blob);
        };
        this.dispose = function () {
            if (url) url = URL.revokeObjectURL(url), undefined;
            this.blob = null;
        };
    }
    o = new Blob();
    for (key in o)
        (function (key) {
            Object.defineProperty(myBlob.prototype, key, {
                enumerable: true,
                configurable: true,
                get: function () {return this.blob[key];}
            });
        }(key));
    o = key = undefined;
    return myBlob;
}());

现在,使用 Blob 创建>新的myBlob(blob) 立即生成blob ,不保留对blob的其他引用。然后,当您完成 Blob 时,请调用 myWrappedBlob.dispose(); ,然后将其释放为GC。如果确实需要将 Blob 直接传递给某些东西,我就给它了属性 myBlob.blob

Now, instead of your regular Blob creation use new myBlob(blob) immediately as you make your blob, keeping no other references to the blob. Then when you're finished with your Blob, call myWrappedBlob.dispose(); and it should free it up to be GC'd. If it's really necessary to pass the Blob into something directly, I gave it the property myBlob.blob.

这篇关于删除Javascript blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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