Sha256: d80c6a93ae1c4c60367dd6757f979f337d22343ef04096e729ee230ff38ab518

Contents?: true

Size: 766 Bytes

Versions: 34

Compression:

Stored size: 766 Bytes

Contents

require 'objspace'

module MemoryUsage
  MemoryReport = Struct.new(:total_memsize)

  def self.full_gc
    GC.start(full_mark: true)
  end

  def self.report(&block)
    rvalue_size = GC::INTERNAL_CONSTANTS[:RVALUE_SIZE]

    full_gc
    GC.disable

    total_memsize = 0

    generation = nil
    ObjectSpace.trace_object_allocations do
      generation = GC.count
      block.call
    end

    ObjectSpace.each_object do |obj|
      next unless generation == ObjectSpace.allocation_generation(obj)
      memsize = ObjectSpace.memsize_of(obj) + rvalue_size
      # compensate for API bug
      memsize = rvalue_size if memsize > 100_000_000_000
      total_memsize += memsize
    end

    GC.enable
    full_gc

    return MemoryReport.new(total_memsize)
  end
end

Version data entries

34 entries across 34 versions & 4 rubygems

Version Path
twitter_ebooks_poll-3.2.3 spec/memprof.rb
twitter_ebooks_poll-3.2.2 spec/memprof.rb
twitter_ebooks_poll-3.2.1 spec/memprof.rb
twitter_ebooks_poll-3.2.0 spec/memprof.rb
foxdear_ebooks-3.1.8 spec/memprof.rb
foxdear_ebooks-3.1.7 spec/memprof.rb
bot_twitter_ebooks-3.3.2 spec/memprof.rb
bot_twitter_ebooks-3.3.1 spec/memprof.rb
bot_twitter_ebooks-3.3.0 spec/memprof.rb
bot_twitter_ebooks-3.2.0 spec/memprof.rb
bot_twitter_ebooks-0.0.0 spec/memprof.rb
twitter_ebooks-3.1.6 spec/memprof.rb
twitter_ebooks-3.1.5 spec/memprof.rb
twitter_ebooks-3.1.4 spec/memprof.rb
twitter_ebooks-3.1.3 spec/memprof.rb
twitter_ebooks-3.1.2 spec/memprof.rb
twitter_ebooks-3.1.1 spec/memprof.rb
twitter_ebooks-3.1.0 spec/memprof.rb
twitter_ebooks-3.0.9 spec/memprof.rb
twitter_ebooks-3.0.8 spec/memprof.rb