Memory size of a hash or other object?

后端 未结 3 918
走了就别回头了
走了就别回头了 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:02

    An alternative way of having a rough estimate of the size of a hash is to convert it into a string and count the number of characters, each character will be a byte.

    hash = {hello:  "world"}
    => {:hello=>"world"}
    hash.to_s
    => "{:hello=>\"world\"}"
    hash.to_s.size
    => 17
    

    Then you can use a characters to bytes/megabytes calculator

提交回复
热议问题