Ruby garbage collect
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 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.
def without_gc
GC.start # start out clean
GC.disable
begin
yield
ensure
GC.enable
end
end
without_gc do
Benchmark.measure { some_code }
end
That said, GC.start
will cause significant slowdowns if you run it repeatedly.