Ruby garbage collect

后端 未结 6 537
死守一世寂寞
死守一世寂寞 2020-12-29 03:34

When would you use this garbage collection method in your Ruby program(s)?

GC.start
6条回答
  •  情书的邮戳
    2020-12-29 04:11

    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.

提交回复
热议问题