Skip to content

Instantly share code, notes, and snippets.

@bjjb
Created April 10, 2014 13:22
Show Gist options
  • Select an option

  • Save bjjb/10381667 to your computer and use it in GitHub Desktop.

Select an option

Save bjjb/10381667 to your computer and use it in GitHub Desktop.
Object profiing, can be enabled/disabled with `kill -27 <pid>` (Ruby 2.1.1 version)
begin
require 'objspace'
trap("PROF") do
if $__PROFILING__
GC.start
filename = Time.now.strftime("objects-%Y%m%d%H%S.json")
ObjectSpace.dump_all(output: File.open(filename,'w'))
ObjectSpace.trace_object_allocations_stop
$__PROFILING__ = false
puts "STOPPED OBJECT TRACE => #{filename}"
else
ObjectSpace.trace_object_allocations_start
$__PROFILING__ = true
puts "STARTED OBJECT TRACE"
end
end
rescue LoadError
puts "Profiling not started, since we're on #{RUBY_VERSION} (< 2.1)"
end
@bjjb
Copy link
Author

bjjb commented Apr 10, 2014

Here's the Ruby 2.0 version

begin                                                                                                                                                                             
  require 'json'                                                                                                                                                                  
  trap("PROF") do                                                                                                                                                                 
    filename = Time.now.strftime("objects-%Y%m%d%H%S.json")                                                                                                                       
    counts = ObjectSpace.each_object.inject(Hash.new(0)) do |h, o|                                                                                                                
      h.tap { |h| h[o.class] += 1 }                                                                                                                                               
    end                                                                                                                                                                           
    puts counts.inspect                                                                                                                                                           
    File.open(filename, 'w') { |f| f << counts.to_json }                                                                                                                          
  end                                                                                                                                                                             
  puts "'kill -27 #$PID' to profile"                                                                                                                                              
rescue                                                                                                                                                                            
  puts $!.to_s                                                                                                                                                                    
end 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment