Do I always need to call URL.revokeObjectURL() explicitly?

The other answer is right, but I thought I should add some info for the sake of completeness.

It mainly depends on what you pass to createObjectURL.

  • If you pass a File selected by the user from an <input type=file>, then the blobURI you created is a direct pointer to the file on the user’s disk, nothing apart this mapping URI-file_path is saved in memory. So in this case, you can create a lot of these without ever revoking it with no real risk.

  • If you pass a Blob (or a File) that you did generate (or which has been fetched), then the Blob has to be stored in memory, and the blobURI will indeed keep being a pointer to this Blob and its data, protecting it from GC, until the document dies. In this case, don’t forget to revoke it when you don’t need it anymore.

  • If you pass a MediaStream coming from the user device, don’t, it’s being deprecated and for good reasons: As for the generated Blob, UAs had to keep the connection to the external device open while the blobURI is alive, and even though the MediaStream was closed, it could happen the connection was still open and lead to unavailability of requesting new connections.

Leave a Comment

tech