Home > memory_debug

memory_debug

Memory_debug is a project mainly written in Ruby, it's free.

simple tool to show how ruby is using memory

Usage

To use memory_debug, run Ruby like this (making sure memory_debug.rb is in the current directory or in some other place in Ruby's include path):

% ruby -rmemory_debug my_script.rb

Or you can require it like this at the top of your Ruby source file:

require 'memory_debug'

To see where your Ruby script has memory leaks, call delta_stats before and after any block you think leaks memory.

Example usage

This is memory_stats_test.rb:

 1 require 'memory_debug'
 2 
 3 memory_stats # => shows table of memory usage
 4 delta_stats # => shows new memory usage since last call of delta_stats (none)
 5 
 6 strings = []
 7 100000.times do |i|
 8   strings << i.to_s
 9 end
10 
11 delta_stats # => will show creation of ~100000 new strings
12 
13 objects = []
14 100000.times do |i|
15   objects << Object.new
16 end
17 
18 delta_stats # => will show creation of 100000 new objects

Output

* * * memory stats * * * called from ["memory_stats_test.rb:3"]
[String, 54508]
[Array, 8565]
[Gem::Requirement, 850]
[Gem::Version, 848]
[Gem::Dependency, 387]
[Class, 227]
[Time, 225]
[Gem::Specification, 223]
[MatchData, 207]
[Regexp, 71]
[Hash, 58]
[Proc, 57]
[File, 23]
[Module, 21]
[Binding, 19]
[Float, 5]
[IO, 3]
[Object, 3]
[Bignum, 2]
[Gem::ConfigFile, 1]
[Gem::SourceIndex, 1]
[Thread, 1]
[Gem::GemPathSearcher, 1]
[ThreadGroup, 1]
[Mutex, 1]
[SystemStackError, 1]
[NoMemoryError, 1]
[fatal, 1]
[Gem::Platform, 1]
* * * delta stats * * * called from ["memory_stats_test.rb:4"]
* * * delta stats * * * called from ["memory_stats_test.rb:11"]
[String, 96100]
[Time, -1]
[Gem::Specification, -1]
[Proc, -1]
[Gem::Requirement, -17]
[Binding, -18]
[Gem::Version, -18]
[File, -21]
[Hash, -42]
[MatchData, -207]
[Array, -3741]
* * * delta stats * * * called from ["memory_stats_test.rb:18"]
[Object, 100000]
[Array, 12]
[String, -1]