Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > when to Run the garbage collector?

Reply
Thread Tools

when to Run the garbage collector?

 
 
gz zz
Guest
Posts: n/a
 
      06-28-2007
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/.

 
Reply With Quote
 
 
 
 
Bill Kelly
Guest
Posts: n/a
 
      06-28-2007
From: "gz zz" <(E-Mail Removed)>
>
> 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





 
Reply With Quote
 
 
 
 
gz zz
Guest
Posts: n/a
 
      06-28-2007
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/.

 
Reply With Quote
 
Bill Kelly
Guest
Posts: n/a
 
      06-28-2007

From: "gz zz" <(E-Mail Removed)>
> 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



 
Reply With Quote
 
Sylvain Joyeux
Guest
Posts: n/a
 
      06-28-2007
On Thursday 28 June 2007, Bill Kelly wrote:
> From: "gz zz" <(E-Mail Removed)>
>
> > 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

 
Reply With Quote
 
Sylvain Joyeux
Guest
Posts: n/a
 
      06-29-2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to free memory ( ie garbage collect) at run time with Python 2.5.1(windows) rfv-370 Python 10 08-29-2007 01:21 PM
Can't Run 'RUN' Windows XP Home Luke O'Malley Computer Support 2 09-01-2005 11:38 PM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM
can run javascript can't run vbscript - WHY duncan ASP .Net 2 10-27-2004 09:31 AM
Anybody remember, Run Buddy Run and Second Hundred Years? Tom McCafferty DVD Video 5 10-02-2004 09:15 PM



Advertisments