Project

General

Profile

Bug #11490 ยป allocation_tracker_bug.rb

chancancode (Godfrey Chan), 08/27/2015 07:38 AM

 
1
require 'objspace'
2

    
3
Kernel.module_eval do
4
  if defined?(gem_original_require)
5
    alias_method :original_require, :gem_original_require
6
  else
7
    alias_method :original_require, :require
8
  end
9

    
10
  def require(path)
11
    original_require(path)
12
  end
13
end
14

    
15
nokogiri_gemspec = Gem::Specification.stubs.find { |gem| gem.name == 'nokogiri' }
16

    
17
nokogiri_gemspec.require_paths.each { |path| $: << File.expand_path(path, nokogiri_gemspec.full_gem_path) }
18

    
19
ObjectSpace.trace_object_allocations { require 'nokogiri' }
20

    
21
allocations = Hash.new(0)
22

    
23
ObjectSpace.each_object do |obj|
24
  allocations["#{ObjectSpace.allocation_sourcefile(obj)}:#{ObjectSpace.allocation_sourceline(obj)}"] += 1
25
end
26

    
27
allocations.to_a.sort_by { |(_,count)| -count }.each do |(source, count)|
28
  puts "#{source} => #{count}"
29
end