Sha256: c2dc6cfbc11d8831ee8d457343905111cbe942b4d74320db82f16c44996692f6

Contents?: true

Size: 1.39 KB

Versions: 50

Compression:

Stored size: 1.39 KB

Contents

class RubyObjectLeakTracker
  attr_reader :previous_count_hash, :current_count_hash

  def initialize
    @previous_count_hash = @current_count_hash = {}    
  end

  def difference_between_runs(basis=@previous_count_hash)
    @difference_between_runs ||= Hash[@current_count_hash.map do |object_class, count|
      [object_class, count - (basis[object_class] || 0)]
    end]
  end

  def total_difference_between_runs
    difference_between_runs(@initial_count_hash).values.inject(0) { |sum, count| sum + count }
  end

  def capture_initial_memory_usage
    capture_memory_usage
    @initial_count_hash = @current_count_hash
  end

  def capture_memory_usage
    @difference_between_runs = nil
    @previous_count_hash = @current_count_hash

    class_to_count = Hash.new { |hash, key| hash[key] = 0 }
    ObjectSpace.each_object { |obj| class_to_count[obj.class] += 1 }

    sorted_class_to_count = class_to_count.sort_by { |k, v| -v }
    @current_count_hash = Hash[sorted_class_to_count]
  end

  def dump_status(logger)
    diff = difference_between_runs
    most_used_objects = current_count_hash.to_a.sort_by(&:last).reverse[0, 20]

    most_used_objects.each do |object_class, count|
      delta = diff[object_class]
      logger.add(log_level(delta), sprintf("\t%s: %d (%+d)", object_class, count, delta))
    end
  end

  private
  def log_level(delta)
    delta > 0 ? Logger::WARN : Logger::DEBUG
  end
end

Version data entries

50 entries across 49 versions & 8 rubygems

Version Path
mfk_openapi_ruby_client-1.0.0 vendor/bundle/ruby/2.7.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.54.15 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.53.79 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.53.17 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.53.3 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.53.1 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.52.121 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.52.92 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.52.79 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.52.5 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.52.0 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.51.93 vendor/bundle/ruby/2.3.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.51.38 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.51.37 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.51.34 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.51.22 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.49.118 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb
cloudsmith-api-0.49.98 vendor/bundle/ruby/2.6.0/gems/ethon-0.12.0/profile/support/ruby_object_leak_tracker.rb