9
\$\begingroup\$

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?

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Krythic
    Oct 2 '16 at 22:29
13
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Also it's pretty-much the ONLY time you should call it - especially on the compact frameworks (XBox, WinPhone). \$\endgroup\$ Mar 15 '12 at 11:48
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Mar 15 '12 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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