Created
December 2, 2016 23:06
-
-
Save tenderlove/a88d9829512252193bb8da76083ecfa1 to your computer and use it in GitHub Desktop.
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 '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