2

Is there anything like an event listener that I can add so I can execute a function whenever a GC occurs?

For example, if I simply wanted to log a line to the console on each GC, how can I do that?

I am fine with a Chrome-specific solution.

Jer
  • 5,468
  • 8
  • 34
  • 41
  • Interesting -- why do you want this? – Cameron Feb 05 '15 at 21:23
  • 1
    A couple reasons - for one, I'd like to be able to see (in real-time) when GCs occur, along with used memory (which I'm currently logging with window.performance.memory). Two - the application I'm writing will ideally have very little garbage created (and thus collected) - maybe a pipe dream, but if I get close to being successful I'll want to know every time a GC occurs, so I plan on logging them all to some persistent data store. – Jer Feb 05 '15 at 21:37

1 Answers1

0

If you want to see what GC is doing over a period of time, you can use the Profile tool in Chrome as described here: How can I log what is being garbage collected in my javascript code?

It won't exactly put lines in your logs, but it might give you the info you need.

Community
  • 1
  • 1
darktrek
  • 132
  • 1
  • 9
  • There is also a way to enable Chrome to allow access to JS data, as detailed in the articled linked here http://stackoverflow.com/questions/15607184/what-is-chrome-doing-with-this-gc-event – lemieuxster Feb 05 '15 at 21:32
  • Thanks - I'm aware of (and learning more about) the Chrome profiler, but I really need to know when they're occurring when the site is in regular use. – Jer Feb 05 '15 at 21:38
  • So you need an event that is triggered on a garbage collection that will call an ajax method back to the server which will update a central log? Or will logging on individual workstations do? – darktrek Feb 06 '15 at 19:12
  • Ideally a generic callback where I can do whatever I want (log locally or make a REST call), but beggars can't be choosers! Why, do you know of a way to log on the workstation? – Jer Feb 06 '15 at 23:45