# New Profiler

**URL:** <https://www.ruby-forum.com/t/new-profiler/86571>\
**Category:** Ruby\
**Created:** [March 24, 2006, 5:55am UTC](https://www.ruby-forum.com/t/new-profiler/86571 "2006-03-24T05:55:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [March 24, 2006, 5:55am UTC](https://www.ruby-forum.com/t/new-profiler/86571/1 "2006-03-24T05:55:44Z")

</div>

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](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…

> **[WebMD - Better information. Better health.](https://www.webmd.com/?NewProfiler)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

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

class Class  
alias\_method :orig\_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 C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
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.
