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 ...
随机推荐
- Django完整的开发一个博客系统
今天花了一些时间搭了一个博客系统,虽然并没有相关于界面的美化,但是发布是没问题的. 开发环境 操作系统:windows 7 64位 Django: 1.96 Python:2.7.11 IDE: Py ...
- 13 SQLiteOpenHelper SQLiteDatabase详解
创建数据库: 1. 创建一个类继承SQLiteOpenHelper 2. 创建继承对象 new SQLiteOpenHelper() 3. 用创建的对象获取可写或者可读的SQLiteDatabase ...
- Java的访问权限详解(3+1)public private protected default
Java使用三个关键字在类的内部设定访问权限:public.private.protected.这些访问指定词(access specifier)决定了紧跟其后被定义的成员(方法或属性)可以被谁使用. ...
- Java进阶(四十)Java类、变量、方法修饰符讲解
Java进阶(四十)Java类.变量.方法修饰符讲解 Java类修饰符 abstract: 将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. final: 将一个类生命为最终(即非继承类) ...
- Android开发学习之路--UI之初体验
之前都是学习Activity,对于布局都没有做过学习,这里就简单学习下吧.下面看下Android Studio下有哪些控件: 这里分为Widgets,Text Fields,Containers,Da ...
- jQuery 异步上传插件 Uploadify302 使用 (JavaEE Spring MVC)
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.而且是Ajax的,省去了自己写Ajax上传功能的麻烦.不过官方提供的实例时php版本的,本文将详细介绍Uploadify ...
- iOS中 UIMPMediaPickerController播放系统音乐
布局如下: 引入框架: #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> 遵循 ...
- Android图片色彩变幻
最近在做图片相关的应用,所以就各方积累到一些常用的操作,一般来说会有多种方式来实现这一功能,比如 采用色度变换 采用ColorMatrix颜色矩阵 采用对像素点的直接操作 等等,今天就复习一下第一种方 ...
- Libgdx 1.6.1发布,跨平台游戏开发框架
Libgdx 1.6.1发布 [1.6.1] 英文原文:http://www.badlogicgames.com/wordpress/?p=3694 译文翻译:宋志辉 - Net.newServerS ...
- linux shell编程语句if、case.
shell学习笔记--if,case shell的控制流结构主要有if语句.for语句.case语句.while语句.until语句这五种,在shell中这些语句的用法有点类似C语言,很容易学会,但也 ...