问题描述
在现代浏览器中,可以将大对象分配为 Blob
,然后通过URL请求访问它。该URL将用于浏览器中其他地方的存储对象(如图像数据)。
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.
浏览器如何知道何时不再需要此URL以及相应的 Blob
数据可以自由地被垃圾收集?
How does the browser know when this URL is no longer needed, and the corresponding Blob
data is free to be garbage collected?
推荐答案
最终清除这个资源,但是它可能会在从内存/磁盘中删除之前的一段时间(几小时或几天)。
The browser will eventually clear up this resource, however it may be some while (hours or days) before it is removed from memory/disk.
如果你想明确地删除对象,你可以通过 revokeObjectURL
。
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;
这篇关于JavaScript Blob对象何时收集垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!