When are JavaScript Blob objects garbage collected?

三世轮回 提交于 2019-12-21 09:16:20

问题


In modern browsers, it's possible to allocate a large object as a Blob, then request access to it via a URL. This URL will serve the stored object (such as an image's data) elsewhere in the browser.

How does the browser know when this URL is no longer needed, and the corresponding Blob data is free to be garbage collected?


回答1:


The browser will eventually clear up this resource, however it may be some while (hours or days) before it is removed from memory/disk.

If you wish to explicitly remove the object, you may do so via revokeObjectURL.

var blob = new Blob([/*JPEG data*/], {type: "image/jpeg"}),
    url = (window.URL || window.webkitURL),
    objectUrl = url.createObjectURL(blob);

// use the object URL, eg:
var img = new Image();

img.onload = function()
{
    // release the object URL once the image has loaded
    url.revokeObjectURL(objectURL);
};

// trigger the image to load
image.src = objectURL;


来源:https://stackoverflow.com/questions/21206823/when-are-javascript-blob-objects-garbage-collected

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