Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL.createObjectURL causes memory leak

Tags:

javascript

I'm trying to display images with URL.createObjectURL. However, it takes so much memory, about 10 times of image size. Here is my code:

var image = new Image();
image.src = URL.createObjectURL(blob);
image.className = 'images';
image.onload = function(){
    URL.revokeObjectURL(this.src);
};
$('.images').appendTo('body');

Is this a browser bug? Or something's wrong with my code?

like image 971
user3925697 Avatar asked Sep 06 '14 08:09

user3925697


People also ask

What does URL createObjectURL do?

The URL. createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.

What does URL createObjectURL return?

createObjectURL is a static method provided by the URL Web API. Returns a DOMString containing a unique blob URL, that is a URL with blob: as its scheme, followed by an opaque string uniquely identifying the object in the browser.

Is createObjectURL Async URL?

createObjectURL is synchronous but it seems to complete almost instantaneously. Also note that calling URL. revokeObjectURL in the image's onload handler breaks "Open image in New Tab" in the image's context menu.


1 Answers

Actually you call URL.revokeObjectURL() when you release an existing object URL which was previously created by calling window.URL.createObjectURL() for optimal performance and memory usage, if there are safe times when you can explicitly unload them.

But you can't save memory through creating object, read this answer, and this helper link.

like image 152
Mohamed Yakout Avatar answered Nov 15 '22 11:11

Mohamed Yakout