Project

memory

0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
Memory profiling routines for Ruby 2.3+
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
 Dependencies

Development

bake-test
>= 0
>= 0

Runtime

~> 0.15
 Project Readme

Memory

A set of tools for profiling memory in Ruby.

Development Status

Features

  • Fast memory capture for million+ allocations.
  • Persist results to disk for vast aggregations and comparisons over time.

Installation

Add this line to your application's Gemfile:

$ bundle add 'memory'

Usage

require 'memory'

report = Memory.report do
	# run your code here
end

report.print

Or, you can use the .start/.stop methods as well:

require 'memory'

sampler = Memory::Sampler.new

sampler.start
# run your code here
sampler.stop

report = sampler.report
report.print

RSpec Integration

memory_sampler = nil
config.before(:all) do |example_group|
	name = example_group.class.description.gsub(/[^\w]+/, "-")
	path = "#{name}.mprof"
	
	skip if File.exist?(path)
	
	memory_sampler = Memory::Sampler.new
	memory_sampler.start
end

config.after(:all) do |example_group|
	name = example_group.class.description.gsub(/[^\w]+/, "-")
	path = "#{name}.mprof"
	
	if memory_sampler
		memory_sampler.stop
		
		File.open(path, "w", encoding: Encoding::BINARY) do |io|
			memory_sampler.dump(io)
		end
		
		memory_sampler = nil
	end
end

config.after(:suite) do
	memory_sampler = Memory::Sampler.new
	
	Dir.glob("*.mprof") do |path|
		memory_sampler.load(File.read(
			path,
			encoding: Encoding::BINARY,
		))
	end
	
	memory_sampler.results.print
end

Raw Object Allocations

before = nil

config.before(:suite) do |example|
	3.times{GC.start}
	GC.disable
	before = ObjectSpace.count_objects
end

config.after(:suite) do |example|
	after = ObjectSpace.count_objects
	GC.enable
	
	$stderr.puts
	$stderr.puts "Object Allocations:"
	
	after.each do |key, b|
		a = before.fetch(key, 0)
		$stderr.puts "#{key}: #{a} -> #{b} = #{b-a} allocations"
	end
end

Contributing

We welcome contributions to this project.

  1. Fork it.
  2. Create your feature branch (git checkout -b my-new-feature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin my-new-feature).
  5. Create new Pull Request.