Today i learned about Ruby Garbage Collection, and do some test
def count_allocated_objects
before = GC.stat(:total_allocated_objects)
yield
after = GC.stat(:total_allocated_objects)
after - before
end
count_allocated_objects {
s = "this is a string"
r = /[a-z]/
} # => 1, so only the string `s` be counted
count_allocated_objects {
s = "this is a string"
s.gsub(/[a-z]/, "")
} # => 6, in this case, is the regex `/[a-z]/` counted ?
As you can see, it looks like the regex /[a-z]/ is not counted by the GC.
is it the Ruby's rule or the GC's stat total_allocated_objects rule ?