Skip to content

Instantly share code, notes, and snippets.

@edisonywh
Created February 12, 2019 13:17
Show Gist options
  • Save edisonywh/c61b3ab50359e68454d87b2c13d5d1a8 to your computer and use it in GitHub Desktop.
Save edisonywh/c61b3ab50359e68454d87b2c13d5d1a8 to your computer and use it in GitHub Desktop.
# Note: This is pretty badly written code, I just wrote it as a quick proof for https://dev.to/edisonywh/how-arrays-work-the-way-arrays-work-3bpg
# To get the growth rate of the array, just look at the jump in memory bytes after every copy. You can see that it's about 1.5~
require 'objspace' # Require this for additional ObjectSpace methods
def memory_calculator
array = Array.new
result = {}
100.times do |i|
array << i
memory = ObjectSpace.memsize_of(array)
memory_in_bytes = memory / 8.0
result["#{memory_in_bytes} bytes"] = {
num_elements: array.size,
total_available_memory: "#{memory_in_bytes} bytes",
}
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment