Module: Memory::Profile
- Defined in:
- lib/memory/profile.rb
Constant Summary collapse
- LOG_FILE =
"/tmp/memory_profile.log"
Class Method Summary collapse
Class Method Details
.report ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/memory/profile.rb', line 9 def self.report Dir.chdir "/tmp" ObjectSpace::garbage_collect sleep 10 # Give the GC thread a chance all = [] ObjectSpace.each_object do |obj| next if obj.object_id == all.object_id all << obj end tally = Hash.new(0) max_obj = nil max_count = 0 all.each do |obj| count = obj.memory_profile_size_of_object if max_count < count max_obj = obj max_count = count end tally[obj.class]+=count end open( LOG_FILE, 'a') do |outf| outf.puts '+'*70 tally.keys.sort{|a,b| if tally[a] == tally[b] a.to_s <=> b.to_s else -1*(tally[a]<=>tally[b]) end }.each do |klass| outf.puts "#{klass}\t#{tally[klass]}" end outf.puts '-'*70 outf.puts "Max obj was #{max_obj.class} at #{max_count}" outf.puts "Maximum object is..." outf.puts max_obj.memory_profile_inspect end end |
.simple_count ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/memory/profile.rb', line 52 def self.simple_count Dir.chdir "/tmp" ObjectSpace::garbage_collect sleep 10 # Give the GC thread a chance tally = Hash.new(0) ObjectSpace.each_object do |obj| next if obj.object_id == tally.object_id tally[obj.class]+=1 end open( LOG_FILE, 'a') do |outf| outf.puts '='*70 outf.puts "Memory::Profile report for #{$0}" outf.puts `cat /proc/#{Process.pid}/status` tally.keys.sort{|a,b| if tally[a] == tally[b] a.to_s <=> b.to_s else -1*(tally[a]<=>tally[b]) end }.each do |klass| outf.puts "#{klass}\t#{tally[klass]}" end end end |