散列或其他对象的内存大小? [英] Memory size of a hash or other object?

查看:49
本文介绍了散列或其他对象的内存大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ruby 1.9.3 中以字节为单位获取给定哈希(或任何对象)大小的最佳方法是什么?

What's the best way to get the size of a given hash (or any object really) in bytes in Ruby 1.9.3?

查找数量的解决方案Ruby 中特定哈希使用的字节数"在 1.9.3 中似乎无效,因为 memsize_of 不在 ObjectSpace.

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.

推荐答案

ObjectSpace.memsize_of 确实有效 在 1.9.3 中,无论是否记录:

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

这篇关于散列或其他对象的内存大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆