在谷歌浏览器中强制垃圾收集 [英] Forcing garbage collection in Google Chrome

查看:61
本文介绍了在谷歌浏览器中强制垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个带有 ZK 的单页网络应用程序,它不断与服务器通信并更新其部分屏幕.更新频率可以达到 1 秒.在这些更新过程中,对大量 JS 对象的引用丢失,这些对象最终必须由垃圾收集器清理.

We are developing a single-page web app with ZK which constantly communicates with server and updates parts of its screens. Updating can be as frequent as 1s. During these updates, references to large ammounts of JS objects are lost and those objects have to be cleaned by garbage collector eventually.

据我们所知,Chrome 仅在非活动选项卡上运行其垃圾收集器.这对我们来说是一个问题,因为应用程序的选项卡通常处于活动状态并且几乎从不刷新,因此永远不会收集 JS 对象.如果保持活动状态足够长,标签最终会崩溃(Aww Snap 消息).

As far as we've figured out, Chrome only runs its garbage collector on inactive tabs. This is a problem for us, because the app's tab is usually active and almost never refreshed, thus JS objects never get collected. If left active for enough time, the tab eventually crashes (Aww Snap message).

我们需要手动启动垃圾收集.到目前为止,我们已经尝试使用 --js-flags="--expose-gc" 运行 Chrome 并运行 gc(),但它抛出了一个异常:

We need to initiate garbage collection manually. So far we've tried running Chrome with --js-flags="--expose-gc" and running gc(), but it throws an exception:

ReferenceError: gc is not defined

这在 Firefox 上不会发生——内存使用或多或少是一个常数.

This doesn't happen on Firefox -- memory usage is more or less a constant.

强制刷新页面不是一种选择.

Force refreshing the page is not an option.

我们将不胜感激任何建议.

We would be grateful for any and all suggestions.

EDIT:我们已经尝试在 Chrome 版本 23.0.1271.97 上运行 window.gc()gc()m25.0.1364.2 dev-m

EDIT: we've tried running window.gc() and gc() both on Chrome versions 23.0.1271.97 m and 25.0.1364.2 dev-m

推荐答案

你可以获取 Chrome Dev Tools 的代码,修改它以便 ProfilerAgent.collectGarbage(); 不时被调用(这是当您单击时间轴"面板上的收集垃圾"按钮并使用 --debug-devtools-frontend 标志使用您的 DevTools 版本运行 Chrome 时调用的代码.

You can fetch code of Chrome Dev Tools, modify it so that ProfilerAgent.collectGarbage(); is called every now and then (it's a code that is called when you click 'Collect Garbage' button on the Timeline panel) and run Chrome with your version of DevTools using --debug-devtools-frontend flag.

然而,这个解决方案相当极端,只有当你真的很绝望时才尝试.到那时,我建议分析您的应用程序并检查为什么 v8 决定不清理垃圾(或无法清理垃圾).DevTools 的时间轴面板将帮助您解决这个问题.首先检查此面板底部的收集垃圾"按钮是否真的起作用,如果没有 - 您可能有内存泄漏(至少,根据 v8).如果是这样,请尝试 leak-finder-for-javascript.

However, this solution is quite extreme, try it only when you get really desperate. Till then, I propose profiling your application and checking out why v8 decides not to clean the garbage (or can't clean the garbage). Timeline panel of DevTools will help you out with this. Start with checking if 'Collect Garbage' button at the bottom of this panel really does its job, if not - you probably have a memory leak (at least, according to v8). If so, try leak-finder-for-javascript.

[EDIT] 我删除了关于 chrome 扩展的信息,因为当 --js-flags 可以从网页代码调用 gc()="--expose-gc" 使用.至少在我的 23.0.1271.64 上.

[EDIT] I removed info about chrome extension, as it turns out that gc() can be called from webpage code when --js-flags="--expose-gc" is used. At least on my 23.0.1271.64.

这篇关于在谷歌浏览器中强制垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆