在Internet Explorer中,我们可以使用此方法强制执行Javascript垃圾收集:CollectGarbage();
Firefox上没有定义该方法。你知道是否有某种等价物吗?
谢谢。
在Internet Explorer中,我们可以使用此方法强制执行Javascript垃圾收集:CollectGarbage();
Firefox上没有定义该方法。你知道是否有某种等价物吗?
谢谢。
(不只是将此答案限制在基于WebKit的浏览器......)
--js-flags="--expose-gc"
,然后它提供 window.gc()
。about:memory
。window.opera.collect()
。window.CollectGarbage()
。请注意,您不应该手动运行GC。我只发布了这个,因为它对开发测试很有用。
我一直试图强制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));
}
当然,一个问题是知道何时停止调用gc(...):因为那个需要一些外部方法来检测GC是否成功结束。在我的情况下,我使用嵌入式WebView,我可以真正检查之前强制下一轮更大的阵列。
我不确定这不是主题,但有一个名为FreeMemory的firefox加载项(https://addons.mozilla.org/en-US/firefox/addon/freememory/)运行垃圾或循环收集而不访问:内存窗格,带有可配置的计时器。我相信其他浏览器还有其他选择。
访问 about:memory
。
来自页面 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