Rack (Per-Request) Memory Usage
Author: Samuel Williams When: Wednesday, 24 August 2011April 2009
May 2009
August 2009
September 2009
October 2009
- Building a Concrete Bath
- LED Lighting Comparison
- Thinking about Programming Languages
- How To Be A Consultant
- Lucid Programming Dojo
- Exim4 + ClamAV + SpamAssassin
- Secure login using AJAX
- Ramaze And Rack
- ActiveMerchant
- Concurrency And Immutability
- Floating Point Numbers
- Programming And Debugging
- Useful jQuery Plugins
- Loading Anonymous Ruby Classes
- 尺八 (Shakuhachi)
- Card Trick
- Object Oriented C
- Gemcutter
- Writing Clearly
- Richard Stallman In Christchurch
- Magnatune
- Client Side Graphing
- Zena CMS
November 2009
February 2010
March 2010
April 2010
May 2010
June 2010
July 2010
August 2010
September 2010
December 2010
January 2011
March 2011
May 2011
August 2011
September 2011
I was recently converting some code from Ruby 1.8.7 to Ruby 1.9.2 and I wanted to check memory usage. I also wanted to check for memory leaks, which can be a problem in garbage collected languages.
The following code can be used to check memory usage after each request. It is designed to be used in conjunction with other rack middleware:
class MemoryUsage
def initialize(app)
print_memory_usage!
@app = app
end
def print_memory_usage!
$stderr.puts "Memory Usage: " + `ps -o rss= -p #{Process.pid}`
end
def call(env)
result = @app.call(env)
GC.start
print_memory_usage!
return result
end
end
use MemoryUsage
Once you've got this up and running, you can use ab to stress test the website. If memory usage keeps on increasing over time for the same load level, you probably have a memory leak.
Comments
Please note, you can leave a comment that uses (limited) XHTML and Textile syntax.