Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Forked from eric/require_tracking.rb
Created January 12, 2010 23:19
Show Gist options
  • Save technicalpickles/275739 to your computer and use it in GitHub Desktop.
Save technicalpickles/275739 to your computer and use it in GitHub Desktop.
if GC.respond_to?(:enable_stats)
module RequireTracking
def require(*args)
start_allocated_size = GC.allocated_size
super
ensure
$require_stats[args.first] += (GC.allocated_size - start_allocated_size)
end
end
Object.send(:include, RequireTracking)
Kernel.send(:include, RequireTracking)
$require_stats = Hash.new { |h,k| h[k] = 0 }
GC.enable_stats
GC.clear_stats
at_exit do
puts "Memory used by file:"
puts " %30s %s" % [ 'File', 'KB' ]
puts " %30s %s" % [ '-------------', '--------' ]
$require_stats.sort_by { |k,v| -v }.slice(0, $require_stats_top || 20).each do |(k,v)|
puts "%30s: %s" % [ k, (v / 1024).to_s.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') ]
end
end
else
puts "Warning: Not running with an interpreter with GC statistics"
end
> ruby -rrequire_tracking -e "require 'active_support'"
Memory used by file:
File KB
------------- --------
rubygems: 5,421
active_support: 4,296
active_support/core_ext: 2,361
active_support/json: 1,161
active_support/json/encoding: 1,063
rubygems/config_file: 921
yaml: 877
yaml/rubytypes: 669
date: 573
active_support/vendor: 524
rubygems/source_index: 490
rubygems/command_manager: 448
commands/abstract_command: 446
rubygems/local_remote_options: 428
builder: 407
uri: 406
rubygems/command: 399
builder/xmlmarkup: 393
json: 383
builder/xmlbase: 346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment