1

In MRI Ruby 2.0, how thorough is GC.start?

Does it try to garbage collect all objects that no longer have a reference to them? Or does it only GC objects if it thinks it's necessary?

I'm trying to track the number of objects of a certain class I have, and it seems to keep on increasing even though I think some of the objects no longer have a reference to them. Using GC.start doesn't fix this. I don't use any C extensions, so that can't be complicating things.

Edit: The problem I was having was the same as in Ruby Symbol#to_proc leaks references in 1.9.2-p180? - objects still existed when I thought they ought to have been garbage collected, and like in that case, the problem was to do with using implicit Symbol -> Proc. However, it would be nice to know if GC.start is expected to garbage collect all objects, or merely collect whatever MRI thinks is necessary to garbage collect.

Community
  • 1
  • 1
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • It’s not clear if you’re using `GC.start` and it’s not having the effect you expect, or if you’re just seeing objects held on to longer than expected and wondering if `GC.start` will help. – Andrew Marshall Oct 18 '13 at 03:32
  • Is this in a plain-old Ruby script, or in Pry/IRB? Are you looking at ObjectSpace or the process’s memory consumption? – Andrew Marshall Oct 18 '13 at 03:38
  • Plain-old. BTW, I think I've got a more specific question about GC coming up. – Andrew Grimm Oct 18 '13 at 03:39
  • I was getting the same issue as http://stackoverflow.com/questions/7263268/ruby-symbolto-proc-leaks-references-in-1-9-2-p180 . Basically, I was thinking that objects weren't getting GC'd when they should be, but it was because I was using Symbol#to_proc in the same way as that question. – Andrew Grimm Oct 18 '13 at 03:57

0 Answers0