ObjectSpace in ruby contains many useful heap debugging utilities.
Since 1.9 ruby has included objspace.so
which
adds even more methods to the ObjectSpace module:
ObjectSpace.each_object{ |o| ... }
ObjectSpace.count_objects #=> {:TOTAL=>55298, :FREE=>10289, :T_OBJECT=>3371, ...}
require 'objspace'
ObjectSpace.memsize_of(o) #=> 0 /* additional bytes allocated by object */
ObjectSpace.count_tdata_objects #=> {Encoding=>100, Time=>87, RubyVM::Env=>17, ...}
ObjectSpace.count_nodes #=> {:NODE_SCOPE=>2, :NODE_BLOCK=>688, :NODE_IF=>9, ...}
ObjectSpace.reachable_objects_from(o) #=> [referenced, objects, ...]
ObjectSpace.reachable_objects_from_root #=> {"symbols"=>..., "global_tbl"=>...} /* in 2.1 */
In 2.1, we've added a two big new features: an allocation tracer and a heap dumper.
Allocation Tracing
Tracking down memory growth and object reference leaks is tricky when you don't know where the objects are coming from.
With 2.1, you can enable allocation tracing to collect metadata about every new object:
require 'objspace'
ObjectSpace.trace_object_allocations_start
class MyApp
def perform
"foobar"
end
end
o = MyApp.new.perform
ObjectSpace.allocation_sourcefile(o) #=> "example.rb"
ObjectSpace.allocation_sourceline(o) #=> 6
ObjectSpace.allocation_generation(o) #=> 1
ObjectSpace.allocation_class_path(o) #=> "MyApp"
ObjectSpace.allocation_method_id(o) #=> :perform
A block version of the tracer is also available .
Under the hood, this feature is built on NEWOBJ
and FREEOBJ
tracepoints
included in 2.1. These events are only available from C, via rb_tracepoint_new()
.
Heap Dumping
To further help debug object reference leaks, you can dump an object (or the entire heap) for offline analysis.
require 'objspace'
# enable tracing for file/line/generation data in dumps
ObjectSpace.trace_object_allocations_start
# dump single object as json string
ObjectSpace.dump("abc".freeze) #=> "{...}"
# dump out all live objects to a json file
GC.start
ObjectSpace.dump_all(output: File.open('heap.json','w'))
Objects are serialized as simple json, and include all relevant details about the object, its source (if allocating tracing was enabled), and outbound references:
{
"address":"0x007fe9232d5488",
"type":"STRING",
"class":"0x007fe923029658",
"frozen":true,
"embedded":true,
"fstring":true,
"bytesize":3,
"value":"abc",
"encoding":"UTF-8",
"references":[],
"file":"irb/workspace.rb",
"line":86,
"method":"eval",
"generation":15,
"flags":{"wb_protected":true}
}
The heap dump produced by ObjectSpace.dump_all
can
be processed by the tool of your choice. You might try a json processor like jq or a json
database . Since the dump contains outbound references for each object, a full object graph can be re-created for deep analysis.
For example, here's a simple ruby/shell script to see which gems/libraries create the most long-lived objects of different types:
$ cat heap.json |
ruby -rjson -ne ' puts JSON.parse($_).values_at("file","line","type").join(":") ' |
sort |
uniq -c |
sort -n |
tail -4
26289 lib/active_support/dependencies.rb:184:NODE
29972 lib/active_support/dependencies.rb:184:DATA
43100 lib/psych/visitors/to_ruby.rb:324:STRING
47096 lib/active_support/dependencies.rb:184:STRING
If you have a ruby application that feels large or bloated, give these new ObjectSpace features a try. And if you end up writing a heap analysis tool or visualization for these
json files, do let me know on twitter .
Ruby 2.1: objspace.so的更多相关文章
-
Ruby笔记
1.数组遍历方法总结 array = (1..10).to_a length = array.length length.times do t print "#{array[t]} &quo ...
-
安装cocoapods遇到两大坑-Ruby版本升级和Podfile的配置
今天安装cocoapods #移除原有ruby源 $ gem sources --remove https://rubygems.org/ #使用可用的淘宝网 $ gem sources -a htt ...
-
Unable to download data from http://ruby.taobao.org/ &; don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
安装cocoapods,记录两个问题! 1.镜像已经替换成了 http://ruby.taobao.org/, 还是不能不能安装cocoapods, 报错:Unable to download dat ...
-
安装了ruby后怎么安装sass
在命令行中输入 ruby -v 查看版本号 先移除默认的https://rubygems.org源,命令为gem sources --remove https://rubygems.org/,按回车 ...
-
ruby 基础知识(一)
突然今天发现一大神的博客:http://www.cnblogs.com/jackluo/archive/2013/01/22/2871655.html 相信初学者会受益颇多 ruby 参考文档 ...
-
ruby 基础知识(二)
ruby 中的动态方法 http://singleant.iteye.com/blog/1680382 Rails 大量使用了符号(symbol).符号看上去很像变量名,不过以冒号作为前缀.符号的例 ...
-
Ruby安装Scss
Ruby安装Scss 引言 已经许久不写HTML了,今天有点以前的东西要改.但是刚装的Windows10,已经没有以前的Web开发环境了.只好重新安装. 结果Webstorm装好后配置Scss出现错误 ...
-
fzf by ruby
fzf by ruby */--> fzf by ruby 1 github地址 https://github.com/junegunn/fzf 2 简介 软件通过匿名管道和grep扩展了bas ...
-
The Safe Navigation Operator (&;.) in Ruby
The most interesting addition to Ruby 2.3.0 is the Safe Navigation Operator(&.). A similar opera ...
随机推荐
-
了解一下C++输入和输出的概念
我们经常用到的输入和输出,都是以终端为对象的,即从键盘输入数据,运行结果输出到显示器屏幕上.从操作系统的角度看,每一个与主机相连的输入输出设备都被看作一个文件.除了以终端为对象进行输入和输出外,还经常 ...
-
[AlwaysOn Availability Groups]排查:AG超过RPO
排查:AG超过RPO 在异步提交的secondary上执行了切换,你可能会发现数据的丢失大于RPO,或者在计算可以忍受的数据都是超过了RPO. 1.通常原因 1.网络延迟太高,网络吞吐量太低,导致Pr ...
-
[译] jQuery 3 有哪些新东西
转自:https://github.com/cssmagic/blog/issues/59 jQuery 的横空出世,至今已有十个年头了,而它的长盛不衰显然不是没有理由的.jQuery 提供了极为友好 ...
-
Java运算符及顺序、选择结构
:运算符(掌握) ()算术运算符 A:+,-,*,/,%,++,-- B:+的用法 a:加法 b:正号 c:字符串连接符 C:/和%的区别 数据做除法操作的时候,/取得是商,%取得是余数 D:++和- ...
-
阿里云利用web直传文件到oss服务器
http://files.cnblogs.com/files/adtuu/oss-h5-upload-js-direct.tar.gz
-
上传系列:ajaxupload.js
ajaxupload.js 上次说了jquery.upload.js,这次再说一下ajaxupload.js,这个其实也比较简答,只有一个JS文件: html代码: $(function () { v ...
-
使用 Infragistics 的 NetAdvantage 组件时替换部分菜单语言的方法
Infragistics 的 NetAdvantage 组件很好用,不过有些自动的菜单默认都是英文的,一直想替换成中文,以下就是研究了一下午整出来的几行代码,貌似网上很难找到此类的相关资料,替换的资源 ...
-
Traffic Manager:Azure中国版 正式发布
我们很高兴地宣布Azure Traffic Manager 现已面向中国版Azure正式发布.此版本现已投入生产,由企业 SLA支持,随时可用于生产场景中. 借助Azure Traffic Ma ...
-
java的Calendar,获取月份少一月的问题及其它注意事项
java的Calendar,获取月份少一月的问题及其它注意事项: import java.util.*; public class DateClass{ public static void main ...
-
sql server 中用于排名的三个函数 row_number() ,RANK() 和 DENSE_RANK()
row_number() ,RANK() 和 DENSE_RANK() 三个配合over() 使用排名 只是只是计算方式不一样,语法基本一样 语法: ROW_NUMBER() OVER (OR ...