In part 1 of this overview I looked at a gui tool to monitor and analyze a live application’s memory usage. This time, let’s turn to a command-line tools that can look at things after-the-fact: the heapy gem.

Ruby version 2.1 introduced the ability to take heap memory dumps using the ObjectSpace module. To do so, just add:

require 'objspace'


…to your application boot. Later on, when you want to take a heap memory dump, you can use:

  output:'heap-dump.json', 'w')).close

You will find a heap-dump.json in your project folder (in production you could upload this file to S3 for later analysis). To analyze it with heapy, just do:

$ gem install heapy
$ heapy read heap-dump.json

By default heapy will show you a list of all GC generations:

Analyzing Heap
Generation: nil object count: 11978
Generation:   6 object count: 74
Generation:   7 object count: 604
Generation:   8 object count: 1778
Generation:   9 object count: 1403
Generation:  10 object count: 3651
Generation:  11 object count: 3047

You can look at stats for a specific generation:

$ heapy read heap-dump.json 6

Analyzing Heap (Generation: 6)

allocated by memory (3867) (in bytes)
  600  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/specification.rb:2040
  456  ~/.rvm/gems/ruby-2.3.1/specifications/pry-0.10.4.gemspec:4
  320  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/requirement.rb:107

object count (74)
  11  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/specification.rb:2040
   8  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/requirement.rb:107
   5  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/specification.rb:1164

High Ref Counts

  33  ~/.rvm/gems/ruby-2.3.1/specifications/pry-0.10.4.gemspec:4
  12  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/specification.rb:1525
   8  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/requirement.rb:107

Duplicate strings

 4  "~>"
 4  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/requirement.rb:107

 2  "method_source"
 1  ~/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/specification.rb:1164
 1  ~/.rvm/gems/ruby-2.3.1/specifications/pry-0.10.4.gemspec:29

Or you can ask for stats for all generations with:

$ heapy read heap-dump.json all

That’s it for heapy, short and sweet!