Is there any way to force the Javascript Garbage Collector in webkit based browsers?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Is there any way to force the Javascript Garbage Collector in webkit based browsers?



In internet explorer we can force the Javascript garbage collection to execute with this method: CollectGarbage();



That method is undefined on Firefox. Do you know if there is some kind of equivalent?



Thanks.





What problem are you really trying to solve? Garbage collection happens on it's own automatically as soon as your JS thread finishes its current thread of execution. Why do you think you need to run garbage collection explicitly?
– jfriend00
Apr 19 '12 at 11:52





Ditto to what jfriend00 said, and you've tagged this as [memory-leaks], are you trying to find memory leaks? What is the actual problem you need to resolve?
– Keldon Alleyne
Apr 19 '12 at 11:58





hi, my problem is that I have an HTML5 games that generates a lot of garbage, and I'd like to call the GC in controlled situations (like pause menu)
– xus
Apr 19 '12 at 12:03




4 Answers
4



(Not just limiting this answer to WebKit-based browsers...)


--js-flags="--expose-gc"


window.gc()


about:memory


window.opera.collect()


window.CollectGarbage()



Note that you shouldn't be manually running the GC. I've only posted this because it's useful for development testing.



I've been just trying to force GC and it seems that regardless of the actual browser relatively good way of doing so is to run following piece of code:


function gc(max)
var arr = ;
for (var i = 0; i < max; i++)
arr.push(i);

return arr.length;

for (var i = 0; ; i++)
// repeat until you have enough:
gc(Math.pow(2, i));



Of course, one issue is to know when to stop calling gc(...): for that one needs some external way to detect the GC is successfully over. In my case I was using embedded WebView and I could really check before forcing next round with bigger array.



I'm not sure if this is not off topic but there is add-on for firefox called FreeMemory (https://addons.mozilla.org/en-US/firefox/addon/freememory/) to run garbage or cycle collection without visiting about:memory pane, with configurable timer. I believe there are alternatives for other browsers out there.



Visit about:memory.


about:memory



From page's documentation on MDN:


about:memory is a special page within Firefox that lets you view, save,
load, and diff detailed measurements of Firefox's memory usage. It also
lets you do other memory-related operations like trigger GC and CC






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard