No commit activity in last 3 years
No release in over 3 years
Saves objectspace to a key-value store after every request
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 0.8
~> 10.0
~> 3.3

Runtime

 Project Readme

rack-objectspace

Build Status

This is my attempt to make a rack middleware that profiles memory usage in rails (or any rack-based app), with the aim of detecting memory leaks. After every request it takes a dump of the objectspace and saves it to a data store of your choice*.

* Any object supporting the []= method can be used as a data-store. I recommend Moneta, which has support for Redis and Amazon S3.

Getting Started

After installing the gem, insert rack-objectspace into your middleware stack:

config.middleware.insert_after ActionDispatch::Static, Rack::Objectspace, store: Moneta.new(:Redis)

I'd recommend installing rack-objectspace as low as you can in your stack. Preferably only requests that hit your own application code should be profiled.

Warnings

  • DO NOT USE THIS IN PRODUCTION (it's VERY slow - I'd recommend running it on a close-copy of production instead)
  • It relies on MRI ruby's ObjectSpace. This won't work in MRI Ruby < 2.1, or any version of JRuby or Rubinius.
  • Every objectspace dump is unique to a particular ruby process. Make sure your server is only running a single worker when profiling.
  • I've found that with unicorn, requests will usually time-out when using rack-objectspace. Try increasing the timeout value in unicorn.rb.

Finding memory leaks

Right now this bit is manual. rack-objectspace will give you the objectspace for a running app. I'd recommend taking the objectspace dumps for several successive requests and looking for objects that aren't garbage collected, as @wagenet did in his post on the Skylight blog.

Credits

This project was heavily inspired by blog posts by @samsaffron and @wagenet. I take no credit for the technique used here.

Also, many thanks to @newbamboo for letting me work on this during office hours.