javascript 删除Javascript blob?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22899333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:59:22  来源:igfitidea点击:

Delete Javascript blobs?

javascriptgoogle-chrome-app

提问by Phil Kulak

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.revokeObjectURLthat 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.

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

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

具体来说,我在这样的块中使用 File 对象:

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

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

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

回答by Paul S.

This looks like you have a memory leak.

这看起来像你有内存泄漏。

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

JavaScript没有您所说的“删除”,它会在属性和变量变得孤立时进行垃圾收集。该delete运营商就是这样的一个方式来做到这一点-它会从一个属性的定义对象。正确
使用delete意味着在属性上使用它,而不是在variable 上使用。它对某些变量起作用的原因是因为var在全局命名空间中发生了什么(即它们成为 的属性window)。这也意味着您不能delete使用参数。

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.

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

Next, consider

接下来考虑

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

What is o.bnow?

o.b现在是什么?

`o.b; // []`

It's still pointing at the Arrayeven though we deleted the o.areference. This means that the Arraywon'tbe garbage collected.

即使我们删除了引用,它仍然指向Arrayo.a。这意味着Array不会被垃圾收集。

So what does this mean for you?

那么这对你的意义是什么?

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

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

Yes, revoking the URIis 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 Blobsso you can at least minimise the problem.

是的,撤销URI是其中的一部分,但您还需要在整个代码中删除引用。如果您觉得这很困难,我建议您将所有Blob都包装起来,这样至少可以最大程度地减少问题。

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;
}());

Now, instead of your regular Blobcreation 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 reallynecessary to pass the Blobinto something directly, I gave it the property myBlob.blob.

现在,不要在创建blob 时立即使用常规的Blob创建,不要保留对 blob 的其他引用。然后当你完成你的Blob 时,调用它应该释放它以进行 GC。如果真的有必要将Blob直接传递给某个东西,我给了它属性。new myBlob(blob)myWrappedBlob.dispose();myBlob.blob