Ruby garbage collect

后端 未结 6 502
死守一世寂寞
死守一世寂寞 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:00

    Usually discouraged unless you have some special need. Ex. sometimes during memory analysis it's useful to force a gc for better predictability.

    0 讨论(0)
  • 2020-12-29 04:00

    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

    0 讨论(0)
  • 2020-12-29 04:02

    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.

    0 讨论(0)
  • 2020-12-29 04:03

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 04:20

    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.

    0 讨论(0)
提交回复
热议问题