Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active September 19, 2016 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/4ea7faf9706bec4894c8a3eb0eb41cd5 to your computer and use it in GitHub Desktop.
Save JoshCheek/4ea7faf9706bec4894c8a3eb0eb41cd5 to your computer and use it in GitHub Desktop.
Getting an MRI heap dump
require 'objspace'
# a class and instance
class A
end
a = A.new
# dump the heap
outfile = 'dump.json'
File.open(outfile, "w") { |f| ObjectSpace.dump_all output: f }
# look through the dump to find the object
system 'bash', '-c', <<SH
function find_class {
# may need to $ brew install jq
jq 'select(.type == "CLASS" and .name == "'"$1"'")' #{outfile}
}
echo ----- The class -----
find_class A
echo
echo ----- The instance -----
find_class A | jq .address | xargs -I {addr} jq 'select(.class == "{addr}")' #{outfile}
SH
----- The class -----
{
"address": "0x7fc9db815ce8",
"type": "CLASS",
"class": "0x7fc9db815cc0",
"name": "A",
"references": [
"0x7fc9db81c958",
"0x7fc9db8c6c78"
],
"memsize": 712,
"flags": {
"wb_protected": true
}
}
----- The instance -----
{
"address": "0x7fc9db815c70",
"type": "OBJECT",
"class": "0x7fc9db815ce8",
"ivars": 0,
"memsize": 40,
"flags": {
"wb_protected": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment