Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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