Objects come into being all the time – most without you writing new. A counter of the GC tells how many: GC.stat(:total_allocated_objects). The first measurement of a new piece of code also counts the caches Ruby makes for it, so the helper warms up once:
def allocations
before = GC.stat(:total_allocated_objects)
yield
GC.stat(:total_allocated_objects) - before
end
allocations { }
{
"1 + 2" => allocations { 1 + 2 },
"1.5 + 2.5" => allocations { 1.5 + 2.5 },
'"a" + "b"' => allocations { "a" + "b" },
":fox" => allocations { :fox },
"[1, 2]" => allocations { [1, 2] }
}The last lesson explains each number. 1 + 2: Fixnums live in the word, nothing is made. 1.5 + 2.5: 1 – here, without flonums, the result is a new Float on the heap (on a laptop: 0). "a" + "b": 3 – each string literal makes a new String, and so does the sum. Someone has to clean up. CRuby's GC is mark and sweep: start at the roots – the VM stack, globals, constants – and mark everything reachable; whatever is left unmarked is garbage, and its slot becomes free. Watch it, on a graph Ruby itself provides: ObjectSpace.reachable_objects_from runs the GC's own marking function for one object.
fox = ["Chunky", ["bacon", "ham"]]
den = { owner: fox }
lost = ["nobody", ["knows", "me"]]
# fox and den are roots; lost is on the heap, but nothing points at it
show_gc({ fox: fox, den: den }, lost)Black: marked. Grey: seen, its references not yet followed. When nothing grey is left, the sweep frees every white slot. Marking the whole heap each time is slow, so CRuby is also generational: an object that survives a GC gets one year older, and at age 3 it is "old" and no longer looked at by the quick, frequent minor GCs – most objects die young, and the old ones are rarely garbage. You can watch an object age:
require "json"
fossil = Object.new
ages = [JSON.parse(ObjectSpace.dump(fossil))["flags"]]
3.times do
GC.start
ages << JSON.parse(ObjectSpace.dump(fossil))["flags"]
end
ages- Old at age 3:
RVALUE_OLD_AGE:746. Mark:gc_marks_start:5766,gc_mark_children:4634; sweep:gc_sweep_page:3552. reachable_objects_fromlends the GC's marking function:rb_objspace_reachable_objects_from:4533, which callsrb_gc_mark_children.
reachable?(root, target) starts at root and follows ObjectSpace.reachable_objects_from until it finds target (the very same object: equal?) or runs out of objects. Keep a list of the objects already marked, or a cycle will keep you going forever. Classes and modules can be skipped.require "objspace"
def reachable?(root, target)
marked = []
grey = [root]
until grey.empty?
obj = grey.shift
# found it? already marked? otherwise mark it and add what it points at
end
false
end
box = ["Chunky", ["bacon"]]
box << box
reachable?(box, box[1][0])That is the whole tour: text → tokens → tree → instructions → a stack of frames → VALUEs in slots → classes and their chains → and back to free slots. For the long version, with much more about hash tables, object shapes and the JIT: Ruby Under a Microscope by Pat Shaughnessy.