在Google Chrome中强制垃圾回收 [英] Forcing garbage collection in Google Chrome

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

问题描述

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



我们已经想通了,Chrome只在非活动选项卡上运行垃圾回收器。这对我们来说是一个问题,因为应用程序的选项卡通常是活动的并且几乎从不刷新,因此 对象永远不会被收集。如果保持活动时间足够长,该选项卡最终会崩溃( Aww Snap 消息)。



我们需要手动启动垃圾回收。到目前为止,我们已经尝试使用 - js-flags = - expose-gc运行Chrome并运行 gc()
$ p $ ReferenceError:gc未定义
>

这不会发生在Firefox上 - 内存使用率或多或少是一个常量。



强制刷新页面不是一个选项。



我们将非常感谢任何和所有建议。



编辑:我们尝试运行 window.gc() gc()都在Chrome版本 23.0.1271.97 m 25.0.1364.2 dev-m


<您可以获取Chrome开发工具代码,对其进行修改,以便调用 ProfilerAgent.collectGarbage(); 每隔一段时间(在时间轴面板上单击收集垃圾按钮时会调用该代码),并使用来运行Chrome和DevTools版本 - debug-devtools -frontend flag。



然而,这个解决方案是相当极端的,只有当你非常绝望时才尝试。直到那时,我建议分析你的应用程序,并检查为什么V8决定不清理垃圾(或不能清理垃圾)。 DevTools的时间线小组将帮助你解决这个问题。从检查这个面板底部的收集垃圾按钮开始,如果真的没有做到 - 你可能有内存泄漏(至少,根据v8)。如果是这样,请尝试泄漏finder-for-javascript



[编辑]我删除了有关Chrome扩展的信息,因为可以调用 gc()使用 - js-flags = - expose-gc时的网页代码。至少在我的23.0.1271.64。


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.

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).

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

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: we've tried running window.gc() and gc() both on Chrome versions 23.0.1271.97 m and 25.0.1364.2 dev-m

解决方案

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.

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] 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.

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

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