Count the number of objects in memory
Let's say you're having memory issues in your Ruby app and you want to get a feel for how many objects are instantied. You can use ObjectSpace!
require 'objspace'
ObjectSpace.each_object(Object).count
# in my typical rails app: 394683
Everything is a child of Object (for the most part), so you have about 394683 objects in memory! That's a lot! You can narrow this down if you like to just String.
ObjectSpace.each_object(String).count
# in my typical rails app: 295261
But let's check something more interesting, like TZInfo::CountryTimezone:
ObjectSpace.each_object(TZInfo::CountryTimezone).count
# in my typical rails app: 348
Neat!
Tweet