Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created December 2, 2016 23:06
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 tenderlove/a88d9829512252193bb8da76083ecfa1 to your computer and use it in GitHub Desktop.
Save tenderlove/a88d9829512252193bb8da76083ecfa1 to your computer and use it in GitHub Desktop.
require 'json'
require 'objspace'
require 'fileutils'
require 'stringio'
require 'pp'
FileUtils.mkdir_p("tmp")
ObjectSpace.trace_object_allocations_start
$addresses = []
def run
string = "i am a string #{rand(0..100)}"
$addresses << "0x#{ (string.object_id << 1).to_s(16) }"
puts "Address of string is: #{$address}"
string.singleton_class.instance_eval do
end
string
return nil
end
run
run
run
run
# ===== CALLING GC HERE =====
GC.start
# ===========================
File.open('tmp/heap.json','w') do |f|
ObjectSpace.dump_all(output: f)
end
output = File.readlines('tmp/heap.json')
actual = output.map.select do |line|
if $addresses.include?(JSON.parse(line)["address"])
puts "Found in heap dump:"
puts " #{line}"
true
end
end.length
expected = 1
result = expected == actual ? "PASS" : "FAIL"
puts "\n#{result} expected: #{expected} actual: #{actual}"
exit(1) unless expected == actual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment