14

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?

2
  • The answer below does not actually pertain to garbage collection, which removes allocated objects from memory, but to freeing disk space. If you are interested in the details of garbage collection, you might investigate: developer.mozilla.org/en-US/docs/Web/JavaScript/… Commented Jan 20, 2014 at 17:27
  • 2
    @Maus, Thanks for the link. To my mind the garbage collector is responsible for cleaning up any resource that is no longer needed. Further, disk is still memory, and is often used as an extension of RAM (virtual memory.) The browser/OS is free to decide whether the blob is stored in the process's working set, or on disk. Commented Jan 20, 2014 at 18:39

1 Answer 1

14

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.