Skip to content

Instantly share code, notes, and snippets.

@asterite
Created September 9, 2011 13:40
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 asterite/1206228 to your computer and use it in GitHub Desktop.
Save asterite/1206228 to your computer and use it in GitHub Desktop.
Require memory tree
$current = {:path => "*ROOT*", :children => [], :mem => 2 ** 30}
if GC.respond_to?(:enable_stats)
module RequireTracking
def require(*args)
node = {:path => args, :children => []}
$current[:children] << node
prev, $current = $current, node
start_allocated_size = GC.allocated_size
super
ensure
node[:mem] = (GC.allocated_size - start_allocated_size)
$current = prev
end
def numeric_thousands_indicators(number)
number.to_s.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
end
end
Object.send(:include, RequireTracking)
Kernel.send(:include, RequireTracking)
$require_stats = Hash.new { |h,k| h[k] = 0 }
$require_stats_top ||= 20
GC.enable_stats
GC.clear_stats
at_exit do
include ActionView::Helpers::NumberHelper
def print_node(node, indent = 0)
if node[:mem] > (2**20)
puts %(#{" " * indent}#{node[:path]}: #{number_to_human_size node[:mem]})
node[:children].each do |child|
print_node child, indent + 2
end
end
end
print_node $current
end
else
puts "Warning: Not running with an interpreter with GC statistics"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment