MEASURING MEMORY USAGE
There are several gems that help with determining where a program allocates memory. The two that I most often use are allocation_tracer and memory_profiler.
Both tools can measure a whole program or they can be turned on and off to only measure certain parts of a program. Either method allows you to determine hotspots in your program and then act on the information. For example, while developing kramdown several years ago I found that the HTML converter class allocated huge amounts of throw-away strings. By changing this hotspot to a better alternative kramdown got faster and used less memory.
To get you started on using these two gems, here are two files that are intended to get pre-loaded using the -r switch of the ruby binary (i.e. ruby -I. -ralloc_tracer myscript.rb).
Credit: https://gettalong.org/blog/2017/memory-conscious-programming-in-ruby.html