# Reducing String duplication in Ruby

DevFeed: [Reducing String duplication in Ruby](<https://devfeed.tech/articles/reducing-string-duplication-in-ruby-41345.md>)

Original publisher: [Read original article](<https://samsaffron.com/archive/2018/02/16/reducing-string-duplication-in-ruby>)

Author: Sam Saffron

Published: 2018-02-16T03:59:29Z

Content type: article

Language: en

Sources: [Sam Saffron](<https://devfeed.tech/sources/sam-saffron.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [Code](<https://devfeed.tech/topics/code.md>), [Processes](<https://devfeed.tech/topics/processes.md>)

Tags: [debug](<https://devfeed.tech/tags/debug.md>), [memory](<https://devfeed.tech/tags/memory.md>), [performance](<https://devfeed.tech/tags/performance.md>), [rails](<https://devfeed.tech/tags/rails.md>), [reduce](<https://devfeed.tech/tags/reduce.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

This article explains how to use the memory_profiler gem to investigate memory usage in Ruby and Rails applications. It focuses on retained String memory, shows how profiling reports distinguish allocated from retained memory, and discusses why duplicated strings can increase heap usage and garbage-collection work.

## Source excerpt

One very common problem Ruby and Rails have is memory usage. Often when hosting sites the bottleneck is memory not performance. At Discourse we spend a fair amount of time tuning our application so self hosters can afford to host Discourse on 1GB droplets. To help debug memory usage I created the memory_profiler gem, it allows you to easily report on application memory usage. I highly recommend you give it a shot on your Rails app, it is often surprising how much low hanging fruit there is. On unoptimized applications you can often reduce memory usage by 20-30% in a single day of work. Memory profiler generates a memory usage report broken into 2 parts: Allocated memory Memory you allocated during the block that was measured. Retained memory Memory that remains in use after the block being measured is executed. So, for example: def get_obj allocated_object1 = "hello " allocated_object2 = "world" allocated_object1 + allocated_object2 end retained_object = nil MemoryProfiler.report do retained_object = get_obj end.pretty_print Will be broken up as: [a lot more text] Allocated String Report ----------------------------------- 1 "hello " 1 blog.rb:3 1 "hello world" 1 blog.rb:5 1 "world" 1 blog.rb:4 Retained String Report ----------------------------------- 1 "hello world" 1 blog.rb:5 As a general rule we focus on reducing retained memory when we want our process to consume less memory and we focus on reducing allocated memory when optimising hot code paths. For the purpose of this blog post I would like to focus on retained memory optimisations and in particular in the String portion of memory retained. How can you get a memory profiler report for your Rails app? We use the following script to profile Rails boot time: if ENV['RAILS_ENV'] != "production" exec "RAILS_ENV=production ruby #{__FILE__}" end require 'memory_profiler' MemoryProfiler.report do # this assumes file lives in /scripts directory, adjust to taste... require File.expand_path("../../config/environment",