問題描述
我們正在開發一個帶有 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.
不能強制刷新頁面.
我們將不勝感激.
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();
時不時地被調用(它是當您單擊時間軸面板上的收集垃圾"按鈕時調用的代碼)并使用您的 DevTools 版本使用 --debug-devtools-frontend
標志運行 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 時可以從網頁代碼中調用
被使用.至少在我的 23.0.1271.64 上.gc()
="--expose-gc"
[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 中強制進行垃圾收集的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!