10

SO, I read a bit on Garbage Collection lately, out of curiosity, and there is something that is repeated quite often: garbage collection can lead to sudden slow down of the program, depending of the "depth" of the collection.

But in video games, there is a moment where the user can't really notice a small lag: loading screen. So, I thought, it might be useful to proceed to full garbage collection during a loading phase (via Collect()), having to wait a second or two longer should not really impact the user.

My question is, is it really useful, or are there no real benefit to doing this?

Jupotter
  • 302
  • 1
  • 9
  • As part of the debug process for my game, I have provided the user an option to call the Garbage Collector at any time. Depending on how this goes, it may also exist in the final build of the game, too. I would suggest calling it during loading screens and generally at any other time where it won't visually interrupt the user. If your game has a death animation which goes to black screen, this would be a perfect opportunity to call the garbage collector, too. – Krythic Oct 02 '16 at 22:29

1 Answers1

13

Yes, call it as the last thing you do when loading.

This will maximize the time between the level starting and the first lag due to a GC.

You should also be trying to minimise allocations in general too though so you don't get any more lags during a level.

See Poor performance on 360 and WP7 and Twin Paths To Garbage Collector Nirvana

Glorfindel
  • 986
  • 1
  • 9
  • 16
George Duckett
  • 2,875
  • 24
  • 30
  • 1
    Also it's pretty-much the ONLY time you should call it - especially on the compact frameworks (XBox, WinPhone). – Jonathan Dickinson Mar 15 '12 at 11:48
  • 1
    @JonathanDickinson: Absolutely. It should only really be called where the user would expect a pause (loading typically, maybe saving too), and definitely not as part of a loop / regular event. – George Duckett Mar 15 '12 at 11:50