5

I create huge blobs in the client which the user can download as files.

The following example is just to demonstrate the issue

var x= "sdfslkdfjklsdjiflskdflsdf" ; for(var i = 0; i<19;i++) x=x+x
var blob = new Blob([x,x,x,x,x,x,x,x,x,x,x,x,x], {
    type : "text/plain"
});

This will use up a lot of memory. When I'm done with the blob I would like to free up that memory. (I'm testing this in IE11 Edge)

Running this script twice will fail as no more memory can be allocated

I've already tried setting blob = null or using delete blob, nothing happens to the memory.

How to free up the memory?

Calthabis
  • 81
  • 1
  • 1
  • 6
  • You could try "delete blob;" – Feathercrown May 06 '16 at 11:28
  • All the answers are correct. Javascript is gargabe-collected, so these are your best alternatives. If you want to have more control over memory, switch to a server-side implementation and use non-managed code. Be prepared for all kinds of hell, though. – Geeky Guy May 06 '16 at 11:54
  • 1
    If ever you are using the `URL.createObjectURL()` method, remember to also call `URL.revokeObjectURL()`when you don't need the object url anymore, or this will take place in your memory, and won't be GCed. – Kaiido May 06 '16 at 12:50
  • @Kaiido the issue is still there even if you only paste the code in the console – Viktor Mellgren May 06 '16 at 13:52
  • @Kaiido I tried running the script and then hard-refresh (ctrl+F5), the memory is still allocated. The only thing that works is closing IE. (Just a reminder that I'm actually running IE11 Edge) – Calthabis May 06 '16 at 14:15
  • @Feathercrown: [Nope!](http://perfectionkills.com/understanding-delete/) – Bergi May 06 '16 at 14:18
  • Since this seems to be tightly coupled to IE, you could try calling the JScript's [CollectGarbage](http://stackoverflow.com/questions/1614662/what-is-the-javascript-method-collectgarbage-when-and-why-should-it-be-used) method after setting the values null. It's not documented well, only (might) work in IE and not even recommended by MS, but I guess its worth a try. `Blob` also seems to have a `close()` method. Not sure what's the support for that but MDN has *"Closes the blob object, possibly freeing underlying resources."* to say about it. Sounds promising to me. – noppa May 06 '16 at 18:10

1 Answers1

2

I think you are going to hit a wall here. Javascript is garbage-collected, which means the best you can do is "unset" variables to effectively mark them for garbage-collection, when the browser thinks it's time.

As charming as it might seem, setting to null will not free the allocated memory right away, but only remove a reference -- freeing memory only possibly later, when garbage-collection actually occurs (given that no other references to the involved object exist):

var blobA = new Blob(...);
var blobB = blobA;
blobA = null;
console.log(blobB); // Blob, the object still exists

Garbage collection is automatic, and there is no way to predict or detect when it happens from within Javascript. There is one way to manually trigger it, however:

window.location.reload();

That's correct, doing a full reload of the whole page will trigger garbage collection, which is effectively the only way to do it AFAIK. You might want to consider this when designing your application.

John Weisz
  • 30,137
  • 13
  • 89
  • 132