New Profiler.

J

John Carter

One of the rules of optimizing OO programs is to create less objects.

Like a Good Green Environmentalist, you should Reduce, Recycle & Reuse.

The next question is what objects are being created.

My memory profiler I posted last year tells you that...
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/139987

It digs into ObjectSpace and sees what objects are currently alive.


Very good for reducing the memory footprint, but not much use for
decreasing garbage collection.

So, here is my nifty NewProfiler which tells where what class of object is
being created....


http://www.rubygarden.org/ruby?NewProfiler

Just slap this code at the head of program and run...

class Class
alias_method :eek:rig_new, :new

@@count = 0
@@stoppit = false
@@class_caller_count = Hash.new{|hash,key| hash[key] = Hash.new(0)}

def new(*arg,&blk)
unless @@stoppit
@@stoppit = true
@@count += 1
@@class_caller_count[self][caller[0]] += 1
@@stoppit = false
end
orig_new(*arg,&blk)
end

def Class.report_final_tally
@@stoppit = true
puts "Number of objects created = #{@@count}"

total = Hash.new(0)

@@class_caller_count.each_key do |klass|
caller_count = @@class_caller_count[klass]
caller_count.each_value do |count|
total[klass] += count
end
end

klass_list = total.keys.sort{|klass_a, klass_b|
a = total[klass_a]
b = total[klass_b]
if a != b
-1* (a <=> b)
else
klass_a.to_s <=> klass_b.to_s
end
}
klass_list.each do |klass|
puts "#{total[klass]}\t#{klass} objects created."
caller_count = @@class_caller_count[ klass]
caller_count.keys.sort_by{|call| -1*caller_count[call]}.each do
|call|
puts "\t#{call}\tCreated #{caller_count[call]} #{klass} objects."
end
puts
end
end
end

END {
b = String.new
Class.report_final_tally
}



John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top