when to Run the garbage collector?

Discussion in 'Ruby' started by gz zz, Jun 28, 2007.

  1. gz zz

    gz zz Guest

    hi,I want to use ruby's garbage collector

    GC.start

    but when use it,and when not use it?

    --
    Posted via http://www.ruby-forum.com/.
    gz zz, Jun 28, 2007
    #1
    1. Advertising

  2. gz zz

    Bill Kelly Guest

    From: "gz zz" <>
    >
    > hi,I want to use ruby's garbage collector
    >
    > GC.start
    >
    > but when use it,and when not use it?


    Garbage collection in ruby is automatic. You can turn it off
    (GC.disable) or turn it on (GC.enable) or tell it to run "right now"
    (GC.start). But normally you just leave it on by default and
    let it collect automatically. You normally don't need to mess with
    it unless you have some special situation.


    Regards,

    Bill
    Bill Kelly, Jun 28, 2007
    #2
    1. Advertising

  3. gz zz

    gz zz Guest

    Thank you.
    I only see rails use the 'GC.start' in my all 'gems'.e.g.
    def test_time_recognition
    n = 10000
    if RunTimeTests
    GC.start
    rectime = Benchmark.realtime do
    n.times do
    rs.recognize_path("content")
    rs.recognize_path("content/list")
    rs.recognize_path("content/show/10")
    rs.recognize_path("admin/user")
    rs.recognize_path("admin/user/list")
    rs.recognize_path("admin/user/show/10")
    end
    end
    puts "\n\nRecognition (RouteSet):"
    per_url = rectime / (n * 6)
    puts "#{per_url * 1000} ms/url"
    puts "#{1 / per_url} url/s\n\n"
    end
    end

    --
    Posted via http://www.ruby-forum.com/.
    gz zz, Jun 28, 2007
    #3
  4. gz zz

    Bill Kelly Guest

    From: "gz zz" <>
    > Thank you.
    > I only see rails use the 'GC.start' in my all 'gems'.e.g.
    > def test_time_recognition
    > n = 10000
    > if RunTimeTests
    > GC.start
    > rectime = Benchmark.realtime do
    > n.times do
    > rs.recognize_path("content")
    > rs.recognize_path("content/list")
    > rs.recognize_path("content/show/10")
    > rs.recognize_path("admin/user")
    > rs.recognize_path("admin/user/list")
    > rs.recognize_path("admin/user/show/10")
    > end
    > end
    > puts "\n\nRecognition (RouteSet):"
    > per_url = rectime / (n * 6)
    > puts "#{per_url * 1000} ms/url"
    > puts "#{1 / per_url} url/s\n\n"
    > end
    > end


    It's doing GC.start there right before the Benchmark starts, just to
    help achieve a more consistent starting point, for measuring the
    elapsed time.

    Notice it only does the GC.start when benchmarks are being run.


    Regards,

    Bill
    Bill Kelly, Jun 28, 2007
    #4
  5. On Thursday 28 June 2007, Bill Kelly wrote:
    > From: "gz zz" <>
    >
    > > Thank you.
    > > I only see rails use the 'GC.start' in my all 'gems'.e.g.
    > > def test_time_recognition
    > > n = 10000
    > > if RunTimeTests
    > > GC.start
    > > rectime = Benchmark.realtime do
    > > n.times do
    > > rs.recognize_path("content")
    > > rs.recognize_path("content/list")
    > > rs.recognize_path("content/show/10")
    > > rs.recognize_path("admin/user")
    > > rs.recognize_path("admin/user/list")
    > > rs.recognize_path("admin/user/show/10")
    > > end
    > > end
    > > puts "\n\nRecognition (RouteSet):"
    > > per_url = rectime / (n * 6)
    > > puts "#{per_url * 1000} ms/url"
    > > puts "#{1 / per_url} url/s\n\n"
    > > end
    > > end

    >
    > It's doing GC.start there right before the Benchmark starts, just to
    > help achieve a more consistent starting point, for measuring the
    > elapsed time.
    >
    > Notice it only does the GC.start when benchmarks are being run.

    It may be difficult to benchmark (or profile for that matter) code under
    GC: if the GC runs, you can get more than 200ms accounted to whichever
    method was being run.

    If you can afford the memory allocation of your benchmarked code, try
    GC.start ; GC.disable before starting the bench.
    --
    Sylvain Joyeux
    Sylvain Joyeux, Jun 28, 2007
    #5
  6. vice.
    > Seems to me it
    > would be better to write benchmarks that run for an amount of time that is
    > sufficient to ensure that the overhead of GC is consistent between runs of
    > the benchmark.

    The problem is that, in real programs, the code snippet will not be the only
    part of the code allocating objects. When the GC runs, you pay for *all* the
    objects present in the system. Measuring the time the GC takes in a
    controlled situation like a benchmark will not give you any information on
    how much impact your code will have w.r.t. GC in a real program.

    > For example, don't write a benchmark that takes 0.001
    > seconds. Write one that takes 30 seconds, or whatever. I'm not experienced
    > enough with ruby yet, to know what a sensible duration is.

    The problem is not the duration, but the amount of objects created.
    IMO, if you're interested in benchmarking GC effects, benchmark your code in
    both time and object allocation. You'll see the kind of tradeoff that one
    solution has w.r.t. another. (ok: it runs 10 times faster but allocates a lot
    of objects)
    --
    Sylvain
    Sylvain Joyeux, Jun 29, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Tillie

    Garbage Collector Debugging

    Rob Tillie, Aug 15, 2003, in forum: ASP .Net
    Replies:
    11
    Views:
    1,743
    JerryK
    Aug 18, 2003
  2. Pyramis
    Replies:
    0
    Views:
    388
    Pyramis
    Jan 25, 2004
  3. Colt

    Garbage collector problem

    Colt, Nov 15, 2003, in forum: Java
    Replies:
    9
    Views:
    672
    Tim Ward
    Nov 18, 2003
  4. bart59
    Replies:
    0
    Views:
    491
    bart59
    Jun 17, 2004
  5. Will Hartung

    Garbage Collector Tuning?

    Will Hartung, Sep 8, 2004, in forum: Java
    Replies:
    3
    Views:
    2,084
    Kevin McMurtrie
    Sep 11, 2004
Loading...

Share This Page