When are JavaScript Blob objects garbage collected?

前端 未结 1 1541
有刺的猬
有刺的猬 2021-02-20 10:11

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 imag

1条回答
  •  南笙
    南笙 (楼主)
    2021-02-20 10:55

    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;
    

    0 讨论(0)
提交回复
热议问题