• Stars
    star
    50
  • Rank 577,127 (Top 12 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Interactive Ruby Heap Snapshot analyzer

Sheap

Sheap is a library for interactively exploring Ruby Heap dumps. Sheap contains a command-line tool and a library for use in IRB.

Some examples of things you can do with Sheap:

  • Find all retained objects between two heap dumps, and analyze them by their properties
  • Inspect individual objects in a heap dump, and interrogate the objects it references and the objects that reference it (reverse references)
  • For a given object, discover all paths back to the root of the heap, which can help you understand why an object is retained.

Why Ruby heap dumps, briefly:

  • Ruby heap dumps are a snapshot of the state of the Ruby VM at a given point in time, which can be useful for understanding memory-related behavior such as bloat and retention issues.
  • The heap contains objects that may be familiar to your application (constants, classes, instances of classes, and primitives like strings and arrays), as well as objects that are internal to the Ruby VM, such as instruction sequences and call caches.
  • Ruby's garbage collector is a mark-and-sweep collector, which means that it starts at the root of the heap and marks all objects that are reachable from the root. It then sweeps the heap, freeing any objects that were not marked. This means that any object that is reachable from the root is retained, and any object that is not reachable from the root is freed. This is why it's useful to find all objects that are retained between two heap dumps (and thus multiple GC runs), inspect their properties, and understand their paths back to the root of the heap.

Installation

You can gem install sheap to get sheap as a library and command line tool. You can also download lib/sheap.rb to a remote server and require it as a standalone file from IRB.

Usage

Using the command line will open an IRB session with the heap loaded. You can then use the $diff, $before, and $after variable to explore the heap.

$ sheap [HEAP_BEFORE.dump] [HEAP_AFTER.dump]

To use directly with IRB:

# $ irb

require './lib/sheap'

# Create a diff of two heap dumps
$diff = Sheap::Diff.new('tmp/heap_before.dump', 'tmp/heap_after.dump')

# Find all retained objects and count by type
$diff.retained.map(&:type_str).tally.sort_by(&:last)
# => [["DATA", 1], ["FILE", 1], ["IMEMO", 4], ["STRING", 4], ["ARRAY", 10000]]

# Find the 4 largest arrays in the 'after' heap dump
>> $diff.after.arrays.sort_by(&:length).last(5)
# =>
# [#<ARRAY 0x100ec0440  (512 refs)>,
#  #<ARRAY 0x100ec9270  (512 refs)>,
#  #<ARRAY 0x100f4b450  (512 refs)>,
#  #<ARRAY 0x11bc6d5b0  (512 refs)>,
#  #<ARRAY 0x11c137960  (10000 refs)>]

# Grab and examine just the largest array
large_arr = $diff.after.arrays.max_by(&:length)
# =>
# #<ARRAY 0x1023effc8
#  type="ARRAY",
#  shape_id=0,
#  slot_size=40,
#  class=#<CLASS 0x100e43350 Array (252 refs)>,
#  length=10000,
#  references=(10000 refs),
#  memsize=89712,
#  flags=wb_protected>

# Is it old?
large_arr.old?
# => false

# Find the first of its references
large_arr.references.first
# =>
# #<ARRAY 0x11c13fdb8
#  type="ARRAY",
#  shape_id=0,
#  slot_size=40,
#  class=#<CLASS 0x100e43350 Array (252 refs)>,
#  length=0,
#  embedded=true,
#  memsize=40,
#  flags=wb_protected>

# Reference that same object by address
$diff.after.at("0x11c13fdb8")
# =>
# #<ARRAY 0x11c13fdb8
#  type="ARRAY",
#  ...

# Show that object's path back to the root of the heap
$diff.after.find_path($diff.after.at("0x11c13fdb8"))
# => [#<ROOT global_tbl (13 refs)>, #<ARRAY 0x1023effc8 (10000 refs)>, #<ARRAY 0x11c13fdb8>]

Generating heap dumps

Sheap on its own will not generate heap dumps for you. Some options for generating heap dumps:

  • ObjectSpace.dump_all(output: open("tmp/snapshot1.dump", "w"))
  • Derailed Benchmarks bundle exec derailed exec perf:heap_diff produces 3 generations of heap dumps.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/sheap. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Sheap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

More Repositories

1

fzy

🔍 A simple, fast fuzzy finder for the terminal
C
2,821
star
2

discard

🃏🗑 Soft deletes for ActiveRecord done right
Ruby
2,093
star
3

vernier

📏 next generation CRuby profiler
Ruby
807
star
4

curl-to-ruby

⬇️ Convert a curl command into ruby's net/http
JavaScript
237
star
5

pub_grub

🍔 A ruby implementation of the PubGrub CDCL-based version solver
Ruby
151
star
6

fzy.js

A javascript port of fzy's scoring algorithm. As seen on GitHub.com!
JavaScript
151
star
7

actionview_precompiler

Precompiles ActionView templates at app boot for Rails 6+
Ruby
143
star
8

meh

🎑 a simple, minimalist, super fast image viewer using raw XLib
C
142
star
9

dkim

✉️ Pure Ruby DKIM signature library with Rails integration
Ruby
89
star
10

execjs-fastnode

⚡ A faster Node.JS integration for Ruby/Rails ExecJS
Ruby
51
star
11

vecx

👾 vecx vectrex emulator - sdl port
C
50
star
12

modelh

Replacement Model M controller board
HTML
39
star
13

rapidjson-ruby

A fast JSON library for Ruby
Ruby
36
star
14

tvart

Upload art to a Samsung "The Frame" TV
JavaScript
28
star
15

clouddns

☁️ A ruby DSL for managing DNS
Ruby
20
star
16

piink

🐷 Raspberry Pi e-paper display in Ruby
Ruby
18
star
17

fzy.rb

A ruby port of fzy's scoring algorithm.
C
12
star
18

ttytest

An acceptance test framework for interactive console applications
Ruby
11
star
19

fast_sqlite

Speeds up tests using sqlite ⚡
Ruby
10
star
20

sdlmap

🌍 A SDL + libcurl OpenStreetMap viewer
C++
10
star
21

dotfiles

my configs
Lua
10
star
22

ESPresso

☕️ Turning code into coffee: ESP32 espresso machine controller
C
9
star
23

bundler-explain

Gives better explanations of conflicts when running bundle update
Ruby
8
star
24

github_fast_changelog

Generate CHANGELOGs using github's v4 GraphQL API
Ruby
8
star
25

jokerss

🤡 Ruby/Rails feed reader designed for self-hosting
Ruby
8
star
26

roaring-ruby

Roaring compressed bitmaps for Ruby
Ruby
7
star
27

plugin.video.gomtv.net

📺 GOMtv.net video addon for XBMC
Python
7
star
28

bccovidpod

BC COVID-19 Updates from Dr. Bonnie Henry - Converted to podcast
HTML
6
star
29

galette

💎🥞 Experimental dependency resolution algorithm for ruby gems
Ruby
5
star
30

arcana

🧙‍♂️ pure-ruby file/libmagic implementation. VERY INCOMPLETE
Ruby
5
star
31

fullwidth

🇯🇵 Convert ASCII to equivalent fullwidth characters
Ruby
5
star
32

json_escape

Ruby
5
star
33

dynamic_locals

A Ruby to Ruby transpiler allowing dynamic local variables
Ruby
5
star
34

hawthos

C
4
star
35

mimemagic_stub

Ruby
4
star
36

c2dm-ruby

ruby interface to google android's Cloud to Device Messaging service
4
star
37

mpvsrv

Web interface and API for remote control of mpv
JavaScript
4
star
38

qrcli

Generates QR codes on the command line using ANSI terminal colors
Ruby
3
star
39

rds_slow_log

Ruby script to dump the slow query log from the mysql.slow_log on Amazon RDS instances
Ruby
3
star
40

noticat

Simple notification daemon and clock for dwm
3
star
41

levelfind

list directories and files in a level order traversal
C++
3
star
42

snek

Ruby
3
star
43

rbexec

Ruby
3
star
44

giff

💎 Compare two .gem files
Ruby
2
star
45

st

My st configuration
C
2
star
46

bundler_wtf

Ruby
2
star
47

fzy-fpm-cookery

Shell
2
star
48

cruby_crash_info

Ruby
2
star
49

fzy-demo

Demo page for fzy.js
JavaScript
2
star
50

hawthjit

An experimental pure-Ruby JIT compiler
Ruby
2
star
51

circdraw

🎨 draws circles on things
C++
2
star
52

homebrew-fzy

Ruby
2
star
53

rabl_to_jbuilder

Convert rabl templates to jbuilder syntax
Ruby
2
star
54

watchmaker

⌚ Runs `make` when files are changed.
Go
2
star
55

dwm

My dwm config
C
2
star
56

hsh

TIME MACHINE A barely functional shell I wrote for university in 2009
C
2
star
57

visdiff-ruby

Compare screenshots from ruby with visdiff.com
Ruby
2
star
58

asmjit-ruby

Ruby wrapper for AsmJit: a lightweight library for machine code generation
C++
2
star
59

adventofcode2017

Ruby
2
star
60

las2heightmap

Converts LAS lidar data to a PNG heightmap (with a weird encoding)
C++
2
star
61

site-example

Build system for my personal blog at https://www.johnhawthorn.com/
Ruby
2
star
62

clocksay

⏰ A dumb thing for a "smart" alarm clock
Go
2
star
63

hawthrss

Ruby
2
star
64

riama

View only the questions and answers from reddit.com/r/iama posts.
Ruby
2
star
65

hawthfrag

Simple filesystem agnostic online file defragmenter for linux
C
2
star
66

imgcat

iterm2 imgcat in Ruby
Ruby
2
star
67

hawthtest

Ruby
1
star
68

svgcal

svgcal
HTML
1
star
69

hawth-rails-template

My template for creating rails applications
Ruby
1
star
70

doscat

Converts code page 437 with ANSI escapes to UTF-8
C
1
star
71

capybara-profile

Ruby
1
star
72

led_test_formatter

Display test runner progress on LED strips
Ruby
1
star
73

muffins

🍪 TIME CAPSULE: A game prototype from 2007 by @jhawthorn and @jarednorman
Python
1
star
74

rtrace

🔵 Time capsule! A ray tracer I wrote in 2009
C++
1
star
75

magnetman

TIME CAPSULE: November 2006 72Hour Game Development Competition entry (modern rewrite)
JavaScript
1
star
76

uncletbag

🍌 Search for quotes from arrested development
JavaScript
1
star
77

jhawthorn

1
star
78

crowfriend

Yet another twitter <-> IRC bridge. For openhack!
Ruby
1
star
79

ruby-capi

testing out publishing doxygen to gh-pages
CSS
1
star
80

toksay

Ruby
1
star
81

futurasky

⌚ A pebble watchface based on futuraweather powered by forecast.io
C
1
star
82

cool-repo

Ruby
1
star
83

colbert

CSS
1
star
84

lune

🌘 An experimental alternative lua syntax
Lua
1
star
85

adventofcode2018

Ruby
1
star
86

quoth

1
star
87

155pod.com

HTML
1
star
88

rapidash

A minimal javascript dashboard
CoffeeScript
1
star
89

colbert-generator

Ruby
1
star
90

mpvctl

mpc, but for mpv
Ruby
1
star
91

untappd-slack

A slack app which posts untappd check-ins
JavaScript
1
star
92

garagedoo.rb

Ruby
1
star
93

cowefficient

Test of Ruby's CoW behaviour
Ruby
1
star
94

v60_drain

A drainage stand for a Hario v60
OpenSCAD
1
star
95

dwmstatus

My status script for dwm. Probably not useful unless you are me. If you are me, hello!
C
1
star
96

pure_profiler

An educational pure ruby sampling profiler - do not use for reals
Ruby
1
star
97

gvl_timing

Ruby
1
star
98

git-pivotal

A composable integration between git and pivotal tracker
Ruby
1
star
99

rbdbg

A ptrace-based debugger written in Ruby
Ruby
1
star