There are occasions when it's necessary to kick it off, but usually it works fine by itself. I've had situations where an app will chew through 1GB of memory if left unchecked, pushing deep into swap, where triggering GC.start intermittently will cut that to 100MB.
The trouble is that calling this method is very expensive and can slow down your application considerably if used aggressively.
When testing a JSON streamer I want to make sure that the memory consumption remains low. Therefore I need to run GC.start before test cases or else they become unpredictable and can even produce false positives.
I'm benchmarking some code that creates a lot of objects, and I noticed that my benchmarks vary wildly. I determined that the spikes were from garbage collection running during my benchmark.
Controlling the process manually gives me more consistent benchmarks.
def without_gc
GC.start # start out clean
GC.disable
yield
GC.enable
end
without_gc do
Benchmark.measure { some_code }
end
That said, GC.start will cause significant slowdowns if you run it repeatedly.
Usually discouraged unless you have some special need. Ex. sometimes during memory analysis it's useful to force a gc for better predictability.
There are occasions when it's necessary to kick it off, but usually it works fine by itself. I've had situations where an app will chew through 1GB of memory if left unchecked, pushing deep into swap, where triggering
GC.start
intermittently will cut that to 100MB.The trouble is that calling this method is very expensive and can slow down your application considerably if used aggressively.
I use it when iterating through large numbers of items on memory constrained environments (Heroku) - I force a
GC.start
every 100 items or so.Real life example:
When testing a JSON streamer I want to make sure that the memory consumption remains low. Therefore I need to run
GC.start
before test cases or else they become unpredictable and can even produce false positives.When loading a CSV with 100k lines it's a must. Otherwise a server runs out of memory and data does not get loaded. See https://stackoverflow.com/a/52722689/6594668
When Benchmarking
I'm benchmarking some code that creates a lot of objects, and I noticed that my benchmarks vary wildly. I determined that the spikes were from garbage collection running during my benchmark.
Controlling the process manually gives me more consistent benchmarks.
That said,
GC.start
will cause significant slowdowns if you run it repeatedly.