when to Run the garbage collector?

G

gz zz

hi,I want to use ruby's garbage collector

GC.start

but when use it,and when not use it?
 
B

Bill Kelly

From: "gz zz said:
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
 
G

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
 
B

Bill Kelly

From: "gz zz said:
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
 
S

Sylvain Joyeux

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.
 
S

Sylvain Joyeux

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top