Ruby 2.1: objspace.so
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 ...
随机推荐
- Swift基础语法(常量变量、数据类型、元组、可选、断言)
本文来自Swift中文开发组,感谢翻译者的分享. 本文将分几部分对Swift对ios的语法做讲解.本文为第一节,主要讲解基础语法. 常量和变量 常量和变量把一个名字(比如maximumNumberOf ...
- Spark技术内幕:Master的故障恢复
Spark技术内幕:Master基于ZooKeeper的High Availability(HA)源码实现 详细阐述了使用ZK实现的Master的HA,那么Master是如何快速故障恢复的呢? 处于 ...
- DVB-C系统中QAM调制与解调仿真
本文简单记录一下自己学习<通信原理>的时候调试的一个仿真DVB-C(Cable,数字有线电视)系统中QAM调制和解调的程序.自己一直是研究"信源"方面的东西,所以对&q ...
- 开源项目——小Q聊天机器人V1.3
小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...
- 深入剖析Tomcat会话机制
1缓存机制 Tomcat默认将Session保存到内存中.但同时,Tomcat也提供了PersistentManager配合不同的Store实现的方式,使Session可以被保存到不同地方(Datab ...
- Android进阶(一)几种网络请求方式详解
Ref:http://blog.csdn.net/zuolongsnail/article/details/6373051 Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面 ...
- 流密码和RC4
定义:流密码是对称密码算法,从明文输入流逐位或逐字节产生密文输出. 实例:使用最广泛的流密码是RC4. 流密码结构: 流密码类似于”一次一密”,不同的是”一次一密”使用的是真正的随机数流,而流密码使用 ...
- Hibernate3 Criteria对象详解
1.序言 Hibernate框架是目前JavaEE软件开发的企业主流框架,学习Hibernate必然要掌握ORM(对象关系映射Object/Relation Mapping)的概念思想, Hibern ...
- ffmpeg.c函数结构简单分析(画图)
前一阵子研究转码的时候看了FFmpeg的源代码.由于ffmpeg.c的代码相对比较长,而且其中有相当一部分是AVFilter有关的代码(这一部分一直不太熟),因此之前学习FFmpeg的时候一直也没有好 ...
- 【Linux 操作系统】 Secure CRT 终端配置 -- 配置语法高亮 光标 和 字体
. 1. Secure CRT 中没有想要的字体 Windows 8 下没有 Courier New 字体, 需要在系统的字体上进行配置, 进入 C:\Windows\Fonts 目录, 下面是目录的 ...