Ruby and Rails: Debugging a Memory Leak

Posted . Visible to the public.

A memory leak is an unintentional, uncontrolled, and unending increase in memory usage. No matter how small, eventually, a leak will cause your process to run out of memory and crash.

If you have learned about a memory leak, looking at the number of Ruby objects by type can help you track it down:

> pp ObjectSpace.count_objects
{:TOTAL=>77855,
 :FREE=>4526,
 :T_OBJECT=>373,
 :T_CLASS=>708,
 :T_MODULE=>44,
 :T_FLOAT=>4,
 :T_STRING=>65685,
 :T_REGEXP=>137,
 :T_ARRAY=>984,
 :T_HASH=>87,
 :T_STRUCT=>12,
 :T_BIGNUM=>2,
 :T_FILE=>3,
 :T_DATA=>203,
 :T_COMPLEX=>1,
 :T_SYMBOL=>29,
 :T_IMEMO=>5008,
 :T_ICLASS=>49}

Look out for a number that keeps growing over time.

Rails

In a Rails application, you can use derailed_benchmarks Show archive.org snapshot to identify and examine memory leaks Show archive.org snapshot .

Resources

Profile picture of Dominik Schöler
Dominik Schöler
Last edit
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2022-09-08 06:56)