How to Profile Ruby 1.9.2 Scripts with Memory Allocation Reports

Is there a way to profile ruby 1.9.2 scripts with memory allocation reports?

Have you tried profiling the GC? Ruby 1.9.2 includes GC::Profiler.

GC::Profiler.enable
GC.start
puts GC::Profiler.report

You may also want to look at ObjectSpace.count_objects.

Allocation tracer which works with Ruby 1.9?

It looks like perftools.rb (https://github.com/tmm1/perftools.rb) can collect information on object allocations, and it works with Ruby 1.9. Unfortunately, it segfaults when I try to use it to trace object allocations!

If the problem is fixed, I will edit this answer.

Is it possible to use ruby-prof to profile memory/allocations in Ruby 2.1.1?

According to a ruby-prof issue we need a patched version of ruby to use the memory profiler.

  • https://github.com/ruby-prof/ruby-prof/issues/166
  • https://github.com/skaes/rvm-patchsets

What profilers are available for Ruby?

There's a built-in profiler available with "-r profile", which I've used happily.
There's also a gem called ruby-prof.

What are your strategies to keep the memory usage low?

  1. Choose date structures that are efficient representations, scale well, and do what you need.
  2. Use algorithms that work using efficient data structures rather than bloated, but easier ones.
  3. Look else where. Ruby has a C bridge and its much easier to be memory conscious in C than in Ruby.

Can I profile my .zshrc/.zshenv?

Try adding this at the beginning of the file:

# set the trace prompt to include seconds, nanoseconds, script name and line number
# This is GNU date syntax; by default Macs ship with the BSD date program, which isn't compatible
PS4='+$(date "+%s:%N") %N:%i> '
# save file stderr to file descriptor 3 and redirect stderr (including trace
# output) to a file with the script's PID as an extension
exec 3>&2 2>/tmp/startlog.$$
# set options to turn on tracing and expansion of commands contained in the prompt
setopt xtrace prompt_subst

and this at the end:

# turn off tracing
unsetopt xtrace
# restore stderr to the value saved in FD 3
exec 2>&3 3>&-

And you should get a detailed log showing the epoch_second.nanosecond time of the execution of each line. Note that GNU date (and OS support) is required to have nanosecond output.

Edit:

added comments

Edit 2:

If you have zsh 4.3.12 or later, you should be able to set PS4 like this instead of using the date command:

zmodload zsh/datetime
setopt promptsubst
PS4='+$EPOCHREALTIME %N:%i> '

which should work on both Linux and OS X to give you nanosecond precision.



Related Topics



Leave a reply



Submit