Skip to content

Instantly share code, notes, and snippets.

@podrezo
Created July 5, 2018 15:04
Show Gist options
  • Save podrezo/809185b59391bb0e6d900aa6f3102517 to your computer and use it in GitHub Desktop.
Save podrezo/809185b59391bb0e6d900aa6f3102517 to your computer and use it in GitHub Desktop.
Show the heap and stack in action using ruby
class MyClass
attr_writer :my_value
end
def my_function(i)
return if i.zero?
my_stack_var = MyClass.new
# "Each slot is big enough to hold one Ruby object"
puts "heap_free_slot -> #{GC.stat[:heap_free_slot]}, stack_size -> #{caller.size}"
my_function(i-1)
end
my_function(5)
puts "Calling garbage collector..."
GC.start
puts "heap_free_slot -> #{GC.stat[:heap_free_slot]}, stack_size -> #{caller.size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment