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.

Revisions

  1. technicalpickles revised this gist Jan 13, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions require_tracking.rb
    Original file line number Diff line number Diff line change
    @@ -54,3 +54,5 @@ def numeric_thousands_indicators(number)
    end
    end
    else
    puts "Warning: Not running with an interpreter with GC statistics"
    end
  2. technicalpickles revised this gist Jan 12, 2010. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions require_tracking.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    #
    # Created by Eric Lindvall <[email protected]>
    #
    # WHAT: Provides a simple overview of memory allocation occuring during a
    @@ -26,28 +25,32 @@ def require(*args)
    ensure
    $require_stats[args.first] += (GC.allocated_size - start_allocated_size)
    end


    def load(*args)
    start_allocated_size = GC.allocated_size
    super
    ensure
    $require_stats[args.first] += (GC.allocated_size - start_allocated_size)
    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
    puts "Memory used by file:"
    puts " %40s %s" % [ 'File', 'KB' ]
    puts " %40s %s" % [ '-------------', '--------' ]
    $require_stats.sort_by { |k,v| -v }.slice(0, $require_stats_top).each do |(k,v)|
    $require_stats.reject {|k,v| v < 1000 }.sort_by { |k,v| -v }.each do |(k,v)|
    puts "%40s: %s" % [ k, numeric_thousands_indicators(v / 1024) ]
    end
    end
    else
    puts "Warning: Not running with an interpreter with GC statistics"
    end
  3. @eric eric revised this gist Jan 4, 2010. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions require_tracking.rb
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,10 @@
    # WHAT: Provides a simple overview of memory allocation occuring during a
    # require.
    #
    # For a longer explanation, see my post at:
    #
    # http://bitmonkey.net/post/308322913/tracking-initial-memory-usage-by-file-in-ruby
    #
    # NOTE: The numbers provided are of self + children, meaning the same will
    # be attributed to multiple files at once.
    #
  4. @eric eric revised this gist Dec 28, 2009. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions require_tracking.rb
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,14 @@
    #
    # Created by Eric Lindvall <[email protected]>
    #
    # Please note: The numbers provided are of self and children, meaning, you
    # won't be able to add them all up to get the total memory usage of the app.
    # WHAT: Provides a simple overview of memory allocation occuring during a
    # require.
    #
    # It also does not take into account memory that was allocated and then freed.
    # NOTE: The numbers provided are of self + children, meaning the same will
    # be attributed to multiple files at once.
    #
    # Also, memory that is no longer referenced (and would be freed) is still
    # taken into account.
    #
    # It is intended to give a simple overview of allocations to track down gross
    # offenders, not give an exact view of your memory usage.
    @@ -27,15 +31,16 @@ def numeric_thousands_indicators(number)
    Object.send(:include, RequireTracking)
    Kernel.send(:include, RequireTracking)

    $require_stats = Hash.new { |h,k| h[k] = 0 }
    $require_stats = Hash.new { |h,k| h[k] = 0 }
    $require_stats_top ||= 20
    GC.enable_stats
    GC.clear_stats

    at_exit do
    puts "Memory used by file:"
    puts " %40s %s" % [ 'File', 'KB' ]
    puts " %40s %s" % [ '-------------', '--------' ]
    $require_stats.sort_by { |k,v| -v }.slice(0, $require_stats_top || 20).each do |(k,v)|
    $require_stats.sort_by { |k,v| -v }.slice(0, $require_stats_top).each do |(k,v)|
    puts "%40s: %s" % [ k, numeric_thousands_indicators(v / 1024) ]
    end
    end
  5. @eric eric revised this gist Dec 28, 2009. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions require_tracking.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,15 @@
    #
    # Created by Eric Lindvall <[email protected]>
    #
    # Please note: The numbers provided are of self and children, meaning, you
    # won't be able to add them all up to get the total memory usage of the app.
    #
    # It also does not take into account memory that was allocated and then freed.
    #
    # It is intended to give a simple overview of allocations to track down gross
    # offenders, not give an exact view of your memory usage.
    #

    if GC.respond_to?(:enable_stats)
    module RequireTracking
    def require(*args)
  6. @eric eric revised this gist Dec 28, 2009. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions require_tracking.rb
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,10 @@ def require(*args)
    ensure
    $require_stats[args.first] += (GC.allocated_size - start_allocated_size)
    end

    def numeric_thousands_indicators(number)
    number.to_s.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
    end
    end

    Object.send(:include, RequireTracking)
    @@ -17,10 +21,10 @@ def require(*args)

    at_exit do
    puts "Memory used by file:"
    puts " %30s %s" % [ 'File', 'KB' ]
    puts " %30s %s" % [ '-------------', '--------' ]
    puts " %40s %s" % [ 'File', 'KB' ]
    puts " %40s %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') ]
    puts "%40s: %s" % [ k, numeric_thousands_indicators(v / 1024) ]
    end
    end
    else
  7. @eric eric revised this gist Dec 28, 2009. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions run.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    > 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
  8. @eric eric created this gist Dec 28, 2009.
    28 changes: 28 additions & 0 deletions require_tracking.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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