- 我想知道如果我创建一个 ruby 对象会消耗多少内存。 Ruby 有什么方法可以告诉吗?
下面的内存消耗有什么不同吗?
users = User.where("created_at > ?", 2.months.ago) # select all fields users = User.select(:user_name).where("created_at > ?", 2.months.ago) # just select one field
最佳答案
你可以使用 ruby-prof
,一个很棒的 ruby 分析器,它会告诉你你的代码正在做什么,包括内存分配。用法非常简单:
require 'ruby-prof'
# Profile the code
result = RubyProf.profile do
...
[code to profile]
...
end
# Print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
它可以输出结果为text , text graph , html graph , call stack和 more .在自述文件中还有关于 profiling rails 的部分应用程序。安装是即时的,所以试试吧:
gem install ruby-prof
关于ruby-on-rails - 如果我创建一个 ruby 对象会消耗多少内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470187/