Memory size of a hash or other object?

ruby

Solution

`ObjectSpace.memsize_of` does work in 1.9.3, documented or not:

puts RUBY_VERSION #=>1.9.3

require 'objspace'

p ObjectSpace.memsize_of("a"*23)    #=> 23 
p ObjectSpace.memsize_of("a"*24)    #=> 24 
p ObjectSpace.memsize_of("a".*1000) #=> 1000
h = {"a"=>1, "b"=>2}
p ObjectSpace.memsize_of(h)         #=> 116

Problem

What's the best way to get the size of a given hash (or any object really) in bytes in Ruby 1.9.3? The solution to "Find number of bytes a particular Hash is using in Ruby" does not appear to be valid in 1.9.3, because `memsize_of` isn't in the documentation for ObjectSpace.

Original source

Related problems