有没有办法在基于 webkit 的浏览器中强制使用 Javascript 垃圾收集器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10227422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:07:45  来源:igfitidea点击:

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

javascriptfirefoxmemory-leakswebkitgarbage-collection

提问by xus

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

在 Internet Explorer 中,我们可以使用以下方法强制执行 Javascript 垃圾收集:CollectGarbage();

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

该方法在 Firefox 上未定义。你知道是否有某种等价物吗?

Thanks.

谢谢。

回答by ZachB

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

(不仅仅是将这个答案限制在基于 WebKit 的浏览器上......)

  • Chrome: if you launch it from a command line/terminal with --js-flags="--expose-gc", then it provides window.gc().
  • Firefox I think requires clicking the "Free memory" buttons in about:memory.
  • Opera has window.opera.collect().
  • Edge has window.CollectGarbage().
  • Safari, unknown.
  • Chrome:如果您从命令行/终端使用 启动它--js-flags="--expose-gc",那么它会提供window.gc().
  • 我认为 Firefox 需要单击about:memory.
  • 歌剧有window.opera.collect()
  • 边有window.CollectGarbage()
  • Safari,未知。

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

请注意,您不应手动运行 GC。我发布这个只是因为它对开发测试很有用。

回答by Jaroslav Tulach

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:

我一直在尝试强制 GC 并且似乎无论实际浏览器如何,这样做的相对较好的方法是运行以下代码:

  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.

当然,一个问题是知道什么时候停止调用 gc(...):因为需要一些外部方法来检测 GC 是否成功结束。在我的情况下,我使用的是嵌入式 WebView,我真的可以在强制下一轮使用更大的数组之前进行检查。

回答by tomasb

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.

我不确定这是否不是题外话,但有一个名为 FreeMemory ( https://addons.mozilla.org/en-US/firefox/addon/freememory/) 的firefox 插件来运行垃圾或循环收集,而无需访问 about:memory 窗格,带有可配置的计时器。我相信有其他浏览器的替代品。

回答by Simha

Visit about:memory.

访问about:memory

From page's documentation on MDN:

从页面上的 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