Skip to content

Instantly share code, notes, and snippets.

@jrafanie
Last active November 30, 2016 17:37
Show Gist options
  • Save jrafanie/9e1d3caeb29339f696b8b6f4eca9efda to your computer and use it in GitHub Desktop.
Save jrafanie/9e1d3caeb29339f696b8b6f4eca9efda to your computer and use it in GitHub Desktop.
Rails boot allocations
def iter(n)
if n > 0
1.times{
iter(n-1)
}
else
# null code
end
end
def gc
3.times do
iter(100)
GC.start
GC.start
end
end
def start_objspace_trace
require "objspace"
ObjectSpace.trace_object_allocations_start
end
def stop_objspace_trace
ObjectSpace.trace_object_allocations_stop
gc
end
def log_retained_allocations(key = :file)
current_file = File.basename(__FILE__)
object_hash = {}
ObjectSpace.each_object do |o|
sourcefile = ObjectSpace.allocation_sourcefile(o) || ""
next if sourcefile.include?(current_file)
obj_key = "#{o.class.name}\t"
obj_key << sourcefile if [:line, :file, :path].include?(key)
obj_key << ":#{ObjectSpace.allocation_sourceline(o)}" if key == :line
object_hash[obj_key] ||= Hash.new { |h, _| h[:count] = 0; h[:memsize] = 0 }
object_hash[obj_key][:count] += 1
object_hash[obj_key][:memsize] += ObjectSpace.memsize_of(o)
end
object_hash.sort_by {|k, v| -v[:memsize]}[0..1000].each do |k, v|
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
end
start_objspace_trace
require 'bundler/setup'
require './config/environment'
stop_objspace_trace
log_retained_allocations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment