Read more

Count number of existing objects in Ruby

Thomas Eisenbarth
October 27, 2015Software engineer at makandra GmbH

Sometimes you want to know exactly how many objects exist within your running Ruby process. Here is how:

stats = {}
ObjectSpace.each_object  {|o| stats[o.class].nil? ? stats[o.class] = 0 : stats[o.class] += 1 }; stats

=> {String=>192038, Array=>67690, Time=>2667, Gem::Specification=>2665, Regexp=>491, Gem::Requirement=>16323, Gem::StubSpecification=>2665, ...}
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Maybe you want to sort it like this:

stats.sort_by {|k,v| v }
Posted by Thomas Eisenbarth to makandra dev (2015-10-27 16:18)