I’ve been working on a Ruby on Rails web application to track and compare Xbox player scores and achievements—a direct ripoff of a similar Perl/Mason app that Ty wrote. I recently discovered that the web site started getting very slow to respond, and Ruby was using up lots of memory.
I did a fair bit of Google research into memory usage and garbage collection in Rails, but couldn’t find any smoking gun. I did come across a really tiny Memory Usage Logger for Rails, that just adds the process memory size to the server log after each request. I added this handy code, and used the Apache Benchmarking Tool to send several hundred web requests to my app, and graphed the results. Memory was definitely increasing linearly with the number of requests.
After doing some basic troubleshooting and code analysis, I was getting nowhere. Instead, I created a new, simple Rails app. Just an empty app, with one model and controller generated with Rails’s scaffolding and default views. This exhibited the same ever-growing memory problem. Weird. Rails is used widely for major web sites, so I knew this couldn’t be a widespread problem. I next installed Ruby Enterprise Edition, which was fortunately a fast and painless process. When I restarted my application and re-ran my stress test, memory was completely flat!
I notice now that although both versions of ruby (standard and Enterprise Edition) are version 1.8.6, they are different patchlevels, so I’m not sure if the memory fix is due to REE or due to some more recent ruby patches. Either way, I’ll probably stick with REE in the future.