53

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

8
  • 6
    "As far as we've figured out, Chrome only runs its garbage collector on inactive tabs" No, that's incorrect. Chrome will run the GC whenever it feels it needs to, whether the tab is active or not. Dec 19, 2012 at 10:37
  • 5
    It's window.gc() but it's not the ultimate solution because AFAIK it works only on debug versions of Chrome. As Crowder pointed out Chrome will run it when needed so if your app leaks memory then what you should review your code to explicitly release allocated objects whenever possible. Dec 19, 2012 at 10:39
  • 4
    @Cerbrus: delete has nothing to do with memory management in JavaScript, except purely as a side-effect if you happen to use it to remove a property which is the only outstanding reference to an object. Dec 19, 2012 at 10:41
  • 2
    @Cerbrus: :-) Just avoiding that myth continuing to be prepetuated (wow Brendan should have called it remove or something, but hey, he was under massive time pressure...). Dec 19, 2012 at 10:45
  • 1
    @T.J.Crowder: We have found an issue in code.google.com, where users also claim that GC doesn't happen unless tab is made inactive (7th comment). It is resolved as WontFix. It most possibly should work as you described, but for some reason doesn't.
    – Paulius K.
    Dec 19, 2012 at 11:26

3 Answers 3

45

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.

3
  • 5
    It turns out you have to close all current Chrome processes before opening one with --js-flags. Now gc() seems to work. The 'Collect Garbage' button also works. Thanks, you've helped a lot.
    – Paulius K.
    Dec 19, 2012 at 14:27
  • 2
    Is there any way to call from javascript?? Apr 22, 2019 at 10:13
  • In cases where you do not own the website in question, but you have control over the client you can install an extension that automatically reloads the page when an Aw Snap error occurred. An example is the Oh No You Didn't extension. Obviously this is a last resort work-around, but it can be useful for some rare use cases :) Jul 21, 2022 at 13:36
41

In Chrome Developer Tools, in the "Performance" tab, find the button that looks like a Garbage Can. Clicking on it forces the garbage collector to run. enter image description here

Note: older Chrome versions from around Chrome 53 had this button in the "Timeline" section, like this: enter image description here

4
  • 15
    Newer versions have moved the Collect garbage button to the Performance tab in Developer Tools: i.stack.imgur.com/tguqy.png
    – Luiz
    Jul 5, 2017 at 15:17
  • yes. you can press it as much as you want. and whenever you want. Mar 2, 2019 at 11:13
  • 1
    I believe in latest Chrome, this trash button is added now in the Memory section next to the record button.
    – aug
    Mar 29, 2019 at 21:53
  • 1
    In mine there seems to be one in both Performance and Memory tabs.
    – SamB
    Aug 7, 2019 at 3:45
5

I found a solution. Apparently Chrome leaks DOM nodes, at least in the current version (26.0.1410.65 right now)

I recorded dev tools timeline in my app and it showed the Event Listeners count going up and down rhythmically along with my app screen's contents, but the DOM Node count was steadily increasing over time, until the tab crashed.

I tried the latest Chrome Canary (28.0.1500.3) and they seem to have fixed the problem. DOM Node count graph now follows the same rhythmic pattern as the Event Listeners.

The thing that gets me is...why doesn't gmail ever crash? I usually keep a tab open for weeks at a time...

1
  • 1
    Well, at least they fixed it. Better later than never :) Thanks for an update.
    – Paulius K.
    May 9, 2013 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.