Quickie: Building Ruby with Memory Profiling
/Ruby's garbage collector has some really interesting memory profiling capabilities. If you build with them turned on, they'll be reported as extra entries in GC.stat.
But how do you turn them on? I mean, without downloading the Ruby source code and configuring everything manually...
If you use rvm, it's pretty easy:
cflags="-D RGENGC_PROFILE=2 -DRGENGC_PROFILE_MORE_DETAIL -DRGENGC_PROFILE_DETAIL_MEMORY -DPROFILE_REMEMBERSET_MARK" rvm install --disable-binary --reconfigure 2.4.1-gcprofile
When you use "rvm --disable-binary --reconfigure" you're making sure it rebuilds Ruby even if it could give you an off-the-shelf binary. When you ask for "2.4.1-whatevername" you're saying to install CRuby 2.4.1 with the name you picked -- above, that name is "gcprofile" because I'm turning on GC profiling. So I can "rvm use ruby-2.4.1-gcprofile" to run with it.
All of that other stuff where I'm setting "cflags" to define a whole bunch of C constants? That's what turns on all the GC profiling. If you think that's a fun thing to do, switch to your new GC-profiling-enabled Ruby, pop into irb, and start checking "GC.stat" after various operations.
There are also some fun things you can do with GC::Profiler:
2.4.1-gcprofile :003 > GC::Profiler.methods.sort - Object.methods
=> [:clear, :disable, :enable, :enabled?, :raw_data, :report, :result, :total_time]
2.4.1-gcprofile :004 > GC::Profiler.enabled?
=> false
2.4.1-gcprofile :005 > GC::Profiler.enable
=> nil
2.4.1-gcprofile :006 > 10_000.times { "bob" + "notbob" }
10 1 27 45
1 6 38 38
=> 10000
2.4.1-gcprofile :007 > GC::Profiler.report
GC 14 invokes.
Index Invoke Time(sec) Use Size(byte) Total Size(byte) Total Object GC Time(ms)
1 0.085 648160 1354560 33864 1.39699999999999535660
2 0.087 0 0 0 0.27699999999995783551
=> nil
2.4.1-gcprofile :008 > GC::Profiler.disable
=> nil
I hope you'll have a little fun checking it out. I am!