Memory size of a hash or other object?

后端 未结 3 926
走了就别回头了
走了就别回头了 2021-02-03 20:47

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

3条回答
  •  你的背包
    2021-02-03 21:06

    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
    

提交回复
热议问题