Hey Vlad, nice to meet you 👋

> I have a curiosity regarding a simple ruby program stolen from this talk https://youtu.be/ZE6F3drGhA8?t=1811
>
> Any hints or learning materials to understand this behavior would be greatly appreciated !

I suspect these might be internal objects that the Ruby VM creates (for things like passing arguments, caches, etc). 

I've been working on a gem to experiment with these kinds of things -- you may want to look at https://github.com/ivoanjo/lowlevel-toolkit/blob/main/examples/track_objects_created.rb and https://github.com/ivoanjo/lowlevel-toolkit/blob/main/ext/lowlevel_toolkit_native_extension/track_objects_created.c for a way to know exactly when Ruby objects get created (and what those objects are -- just be careful that not all of them can be returned back to Ruby code).

Hopefully this gives a starting point for your investigation? ;)

-- Ivo

On Thu, Apr 3, 2025 at 10:16 PM vlad.a.brb--- via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:
Hi everyone,

I have a curiosity regarding a simple ruby program stolen from this talk https://youtu.be/ZE6F3drGhA8?t=1811

<<EOF
def allocations
  x = GC.stat(:total_allocated_objects)
  yield
  GC.stat(:total_allocated_objects) - x
end

p allocations { 1 }
p allocations { 1 }
EOF

Running this program with ruby version 2.7.8 works as I imagined reading it - both calls should print "0" to stdout (considering no allocations are happening in the provided block). However, running it with any ruby version starting from 3.0.7 (may not be the exact one introducing the behavior just what i tested with) I get strange results: first call to allocations outputs 1 and then any other future call to it outputs 0 as I initially expected. I get even stranger results with something like:

<<EOF
class A end
p allocations { A.new }
p allocations { A.new }
EOF

Ruby v2.7.8 outputs 1 for both calls, however from 3.0.7 onwards I see 5 being printed at the first call followed by 2 for future calls.

Any hints or learning materials to understand this behavior would be greatly appreciated !

Have a nice rest of the day / evening depending on your timezone
 ______________________________________________
 ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
 To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
 ruby-talk info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-talk.ml.ruby-lang.org/