heap_inspect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3214d781e181baf1e797e224b047f6230795d8d6
4
+ data.tar.gz: c584ac953a48882b2f3899c79d8b6db0bea32f90
5
+ SHA512:
6
+ metadata.gz: 801576c0ce7cd8ff9c1b995bad59accecf9709d4a47c6f5b0cf281523b89039d1bc7d0aaf6879cf891bb2d9992e08f372b54b27667e9d566428ef5d2a142fe0a
7
+ data.tar.gz: 955311f1133ca21286dad1f11e950ac03db9b6b5f0e085e41038ab6e9916b4d39408ad76229a24272824c3c163a6e063b6830a0a319ea7705fa62a50bc54103c
Binary file
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in heap_inspect.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 schneems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # HeapInspect
2
+
3
+ A CLI for analyzing Ruby Heap dumps. Thanks to [Sam Saffron](http://samsaffron.com/archive/2015/03/31/debugging-memory-leaks-in-ruby) for the idea and initial code.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'heap_inspect'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install heap_inspect
20
+
21
+ ## Usage
22
+
23
+ Step 1) Generate a heap dump. You could [do this manually](http://samsaffron.com/archive/2015/03/31/debugging-memory-leaks-in-ruby). Or you can use a tool like [derailed_benchmarks](https://github.com/schneems/derailed_benchmarks)
24
+
25
+ Step 2) Once you've got the heap dump, you can analyze it using this CLI:
26
+
27
+ ```
28
+ $ heap_inspect read tmp/2015-10-01T10:18:59-05:00-heap.dump
29
+
30
+ Generation: 0 object count: 209191
31
+ Generation: 14 object count: 407
32
+ Generation: 15 object count: 638
33
+ Generation: 16 object count: 748
34
+ Generation: 17 object count: 1023
35
+ Generation: 18 object count: 805
36
+ # ...
37
+ ```
38
+
39
+ Generally early generations will have a high object count as an app is initialized. Over time however, the object count should stabalize. If however you see it spike up, you can drill down into a specific generation. In the previous example, the 17'th generation looks strangely large, you can drill into it:
40
+
41
+
42
+ ```
43
+ $ heap_inspect read tmp/2015-10-01T10:18:59-05:00-heap.dump 17
44
+ Analyzing Heap (Generation: 17)
45
+ -------------------------------
46
+
47
+ allocated by memory (in bytes)
48
+ ==============================
49
+ /Users/richardschneeman/Documents/projects/codetriage/app/views/layouts/application.html.slim:1 (Memory: 377065, Count: 1 )
50
+ /Users/richardschneeman/.gem/ruby/2.2.3/gems/actionview-4.2.3/lib/action_view/template.rb:296 (Memory: 35814, Count: 67 )
51
+ /Users/richardschneeman/.gem/ruby/2.2.3/gems/activerecord-4.2.3/lib/active_record/attribute.rb:5 (Memory: 30672, Count: 426 )
52
+ ```
53
+
54
+ You can also use T-Lo's online JS based [Heap Analyzer](http://tenderlove.github.io/heap-analyzer/) for visualizations.
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `$ bundle install` to install dependencies. Then, run `rake spec` to run the tests.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/heap_inspect. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
70
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "heap_inspect"
5
+
6
+
7
+ HeapInspect::CLI.new(ARGV).run
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'heap_inspect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "heap_inspect"
8
+ spec.version = HeapInspect::VERSION
9
+ spec.authors = ["schneems"]
10
+ spec.email = ["richard.schneeman@gmail.com"]
11
+
12
+ spec.summary = %q{Inspects Ruby heap dumps}
13
+ spec.description = %q{Got a heap dump? Great. Use this tool to see what's in it!}
14
+ spec.homepage = "https://github.com/schneems/heap_inspect"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,133 @@
1
+ require 'json'
2
+
3
+ require "heap_inspect/version"
4
+
5
+ module HeapInspect
6
+
7
+ class CLI
8
+ def initialize(argv)
9
+ @cmd = argv.shift
10
+ @file = argv.shift
11
+ @number = argv.shift
12
+ @args = argv
13
+ end
14
+
15
+ def help
16
+ puts <<-HALP
17
+ $ heap_inspect read <file|command> <number>
18
+
19
+ When run with only a file, it will output the generation and count pairs:
20
+
21
+ $ heap_inspect read tmp/2015-09-30-heap.dump
22
+ Generation: 0 object count: 209191
23
+ Generation: 14 object count: 407
24
+ Generation: 15 object count: 638
25
+ Generation: 16 object count: 748
26
+ Generation: 17 object count: 1023
27
+ Generation: 18 object count: 805
28
+
29
+ When run with a file and a number it will output detailed information for that
30
+ generation:
31
+
32
+ $ heap_inspect read tmp/2015-09-30-heap.dump 17
33
+
34
+ Analyzing Heap (Generation: 17)
35
+ -------------------------------
36
+
37
+ allocated by memory (in bytes)
38
+ ==============================
39
+ /Users/richardschneeman/Documents/projects/codetriage/app/views/layouts/application.html.slim:1 (Memory: 377065, Count: 1 )
40
+ /Users/richardschneeman/.gem/ruby/2.2.3/gems/actionview-4.2.3/lib/action_view/template.rb:296 (Memory: 35814, Count: 67 )
41
+ /Users/richardschneeman/.gem/ruby/2.2.3/gems/activerecord-4.2.3/lib/active_record/attribute.rb:5 (Memory: 30672, Count: 426 )
42
+
43
+ HALP
44
+ end
45
+
46
+ def run
47
+
48
+ case @cmd
49
+ when "--help"
50
+ help
51
+ when nil
52
+ help
53
+ when "read"
54
+ if @number
55
+ Analyzer.new(@file).drill_down(@number)
56
+ else
57
+ Analyzer.new(@file).analyze
58
+ end
59
+ else
60
+ help
61
+ end
62
+ end
63
+ end
64
+
65
+ class Analyzer
66
+ def initialize(filename)
67
+ @filename = filename
68
+ end
69
+
70
+ def drill_down(generation)
71
+ puts ""
72
+ puts "Analyzing Heap (Generation: #{generation})"
73
+ puts "-------------------------------"
74
+ puts ""
75
+
76
+ generation = Integer(generation)
77
+ data = []
78
+ File.open(@filename) do |f|
79
+ f.each_line do |line|
80
+ parsed = JSON.parse(line)
81
+ data << parsed if parsed["generation"] == generation
82
+ end
83
+ end
84
+
85
+
86
+ # /Users/richardschneeman/Documents/projects/codetriage/app/views/layouts/application.html.slim:1"=>[{"address"=>"0x7f8a4fbf2328", "type"=>"STRING", "class"=>"0x7f8a4d5dec68", "bytesize"=>223051, "capacity"=>376832, "encoding"=>"UTF-8", "file"=>"/Users/richardschneeman/Documents/projects/codetriage/app/views/layouts/application.html.slim", "line"=>1, "method"=>"new", "generation"=>36, "memsize"=>377065, "flags"=>{"wb_protected"=>true, "old"=>true, "long_lived"=>true, "marked"=>true}}]}
87
+
88
+ memsize_hash = {}
89
+ data.group_by { |row| "#{row["file"]}:#{row["line"]}" }.
90
+ each do |(k, v)|
91
+ memsize_hash[k] = {
92
+ count: v.count,
93
+ memsize: v.inject(0) { |sum, obj| sum + Integer(obj["memsize"]) }
94
+ }
95
+ end
96
+
97
+
98
+ puts "allocated by memory (in bytes)"
99
+ puts "=============================="
100
+ memsize_hash.sort {|(k1, v1), (k2, v2)| v2[:memsize] <=> v1[:memsize] }.
101
+ each do |k,v|
102
+ puts "#{k} (Memory: #{v[:memsize]}, Count: #{v[:count]} ) "
103
+ end
104
+
105
+ puts ""
106
+ puts "object count"
107
+ puts "============"
108
+ memsize_hash.sort {|(k1, v1), (k2, v2)| v2[:count] <=> v1[:count] }.
109
+ each do |k,v|
110
+ puts "#{k} (Memory: #{v[:memsize]}, Count: #{v[:count]} ) "
111
+ end
112
+ end
113
+
114
+ def analyze
115
+ puts ""
116
+ puts "Analyzing Heap"
117
+ puts "=============="
118
+
119
+ data = []
120
+ File.open(@filename) do |f|
121
+ f.each_line do |line|
122
+ data << JSON.parse(line)
123
+ end
124
+ end
125
+
126
+ data.group_by{ |row| row["generation"] }.
127
+ sort { |a, b| a[0].to_i <=> b[0].to_i }.
128
+ each do |k,v|
129
+ puts "Generation: #{k || " 0"} object count: #{v.count}"
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,3 @@
1
+ module HeapInspect
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heap_inspect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - schneems
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Got a heap dump? Great. Use this tool to see what's in it!
56
+ email:
57
+ - richard.schneeman@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".DS_Store"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/heap_inspect
72
+ - heap_inspect.gemspec
73
+ - lib/heap_inspect.rb
74
+ - lib/heap_inspect/version.rb
75
+ homepage: https://github.com/schneems/heap_inspect
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.5.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Inspects Ruby heap dumps
99
+ test_files: []