Created
October 1, 2015 20:56
-
-
Save kbrock/0521d8cd2ae8b931cbd5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
| ObjectSpace.trace_object_allocations_start | |
| def log_object_space | |
| ObjectSpace.trace_object_allocations_stop | |
| object_hash = {} | |
| ObjectSpace.each_object do |o| | |
| key = "#{ObjectSpace.allocation_sourcefile(o)}" #:#{ObjectSpace.allocation_sourceline(o)}" | |
| object_hash[key] ||= Hash.new { |h, _| h[:count] = 0; h[:memsize] = 0 } | |
| object_hash[key][:count] += 1 | |
| object_hash[key][:memsize] += ObjectSpace.memsize_of(o) | |
| end | |
| #object_hash.sort_by {|k, v| -v[:count]}.each {|k, v| puts "count: #{v[:count]}, #{k}, memsize: #{v[:memsize]}"} | |
| object_hash.sort_by {|k, v| -v[:memsize]}[0..20].each do |k, v| | |
| k = k.gsub(/.*[y\/]gems\//,'') | |
| puts "m: %11s | c: %7s | %s" % | |
| [v[:memsize].to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 "), | |
| v[:count].to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 "), | |
| k ] | |
| end | |
| nil | |
| ensure | |
| ObjectSpace.trace_object_allocations_start | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment