Reputation: 17512
When would you use this garbage collection method in your Ruby program(s)?
GC.start
Upvotes: 37
Views: 22411
Reputation: 125942
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.
Upvotes: 27
Reputation: 2376
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
Upvotes: 3
Reputation: 25092
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.
Upvotes: 1
Reputation: 17516
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.
Upvotes: 15
Reputation: 211600
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.
Upvotes: 30
Reputation: 5296
Usually discouraged unless you have some special need. Ex. sometimes during memory analysis it's useful to force a gc for better predictability.
Upvotes: 7