Memory Cost of Ruby Objects

To measure how much an object cost ObjectSpace:

require "objspace"
ObjectSpace.memsize_of(object)

To measure how much memory a method spent:

def measure
  MemoryProfiler.start;
  yield
  MemoryProfiler.stop
end
VALUE is 40 bytes

A Heroku 1X Dyno is 512MB = 512*1024*1024 = 536870912 Bytes

the_nil = nil
integer = 42
small_integer = 2**62-1
large_integer = 2**62
extra_large_integer = 2**192
extra_extra_large_integer = 2**224
boolean_true = true
boolean_false = false

hash = {}

array = []
big_array = [*1..100]
large_array = [*1..10000]
extra_large_array = [*1..1000000]

string = ""
large_string = "a"*100
extra_large_string = "a"*10000

struct = Struct.new(:a)
struct2 = Struct.new(:a, :b)
struct3 = Struct.new(:a, :b, :c)
struct4 = Struct.new(:a, :b, :c, :d)
struct5 = Struct.new(:a, :b, :c, :d, :e)
struct6 = Struct.new(:a, :b, :c, :d, :e, :f)

ostruct = OpenStruct.new(:a)
ostruct2 = OpenStruct.new(:a, :b)
ostruct3 = OpenStruct.new(:a, :b, :c)
ostruct4 = OpenStruct.new(:a, :b, :c, :d)
ostruct5 = OpenStruct.new(:a, :b, :c, :d, :e)
ostruct6 = OpenStruct.new(:a, :b, :c, :d, :e, :f)

hash = {}
hash1 = { a: 1 }
hash2 = { a: 1, b: 2 }
hash3 = { a: 1, b: 2, c: 3 }
hash4 = { a: 1, b: 2, c: 3, d: 4 }

# RString
str = ""
str1 = "a"
str2 = "a"*24
str3 = "a"*25
str4 = "a"*26

# A class
class User
  def initialize(name)
    @name = name
  end
end

class User2
  attr_reader :name
  def initialize(name)
    @name = name
  end
end