0

Can someone give me an example about a situation when call GC.start is a good idea? I was reading the GC class documentation, but I can't imagine a good situation to use it...

Pedro Vinícius
  • 476
  • 6
  • 13
  • 2
    As with most languages it is generally considered poor form to force a garbage collection and even by doing so you are only forcing it to mark objects (not necessarily immediately clean them up). That being said there are always one off cases where this might be a useful consideration but these situations are usually rare and very specific. (Benchmarking is one of the few normal ones I have heard of just for the sake of predictability one might start the GC and then disable it prior to Benchmarking) – engineersmnky Jul 18 '16 at 19:46

1 Answers1

0

Only in very specific situations when you want to write high-performance code and you know exactly what you are doing.

You have to notice that while the garbage collector is very convenient and safes developers a lot of trouble, it will always be slower than non garbage collected implementations - if these implementations are developed properly that is. A garbage collector will interrupt your program in certain intervals and do a clean-up before resuming the program. These interruptions cause a delay of course. So if you want to squeeze out the last bit of performance then you can think about disabling the automatic garbage collection and doing things manually.

You could also leave the garbage collector in active mode and trigger it manually just before any critical operation, hoping it will then not interrupt your program during this operation, because it already cleaned up.

But I wouldn't meddle with these things unless you absolutely have to. The delay will in most cases be virtually unnoticeable and I can't think of any trouble I ever had with the garbage collector. It just works and is a great tool to have.

Peter
  • 1,361
  • 15
  • 19
  • 1
    Oh. As you were asking for a concrete example: Say you wanted to develop a software for a robotic arm that catches a ball in ruby. Now once the ball is thrown you calculate the trajectory and move the arm to catch it. Here you probably should call GC.Start before the catch operation because you wouldn't want the arm to stop mid-action even for a split second - this could ruin the catch. But normally you'd turn of garbage collection completely for such use-cases. – Peter Jul 18 '16 at 20:04