Last active
September 19, 2016 22:40
-
-
Save JoshCheek/4ea7faf9706bec4894c8a3eb0eb41cd5 to your computer and use it in GitHub Desktop.
Getting an MRI heap dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----- 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