Skip to content

Instantly share code, notes, and snippets.

@drbrain
Last active October 19, 2020 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbrain/0ec0f04b8b9575df2838983c320dd72f to your computer and use it in GitHub Desktop.
Save drbrain/0ec0f04b8b9575df2838983c320dd72f to your computer and use it in GitHub Desktop.
Ruby frozen string interning
$ ruby not_frozen.rb
400600
# The string "a" is embedded inside the String object which is 40 bytes
#
# So 400600/40 = ~10,015 string objects
#
# (IIRC, up to 24 bytes of string may be embedded in the String)
$ ruby frozen.rb
1280
# Strings may be created elsewhere so it is hard to be exact
#
# Still, this is much less than 400KB of String objects
# frozen_string_literal: true
require "objspace"
GC.start
before = ObjectSpace.count_objects_size
array = Array.new(10_000) { "a" }
after = ObjectSpace.count_objects_size
p after[:T_STRING] - before[:T_STRING]
# frozen_string_literal: false
require "objspace"
GC.start
before = ObjectSpace.count_objects_size
array = Array.new(10_000) { "a" }
after = ObjectSpace.count_objects_size
p after[:T_STRING] - before[:T_STRING]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment