Tue, 22 Aug 06

Rails leaking some strings in development

I spent some time this morning checking out Scott Laird’s memory profiler against a fresh rails app.

I was suprised to see that around 110 strings were being leaked on each request. I was incredibly surprised when I used the string_debug option (to the memory profiler) to generate logs of strings living in ObjectSpace. There were strings appearing that I knew were not in my new rails app anywhere.

After grepping my working directory for some of these strings (and not coming up with anything), James suggested that the strings could be in my irb history. Although it wasn’t immediately obvious how they were appearing, the strings were indeed in my history (just over 100 lines worth.)

A little inspection and the culprit was found. The rails breakpoint library requires irb, which loads the irb_history.

In summary: the contents of irb_history are loaded into object space on each request made to your rails app, while in development. These strings do not appear to get garbage collected. The problem does not affect apps running in production as the breakpoint library is not loaded in that environment.