Is there any function in ruby, to find memory used by ruby object.
Similar to how C has the sizeof()
function and PHP has the memory_get_usage()
function. Does ruby have an equivalent function/method?
ObjetSpace#memsize_of
from the Ruby docs:
Return consuming memory size of obj.
[1] pry(main)> require 'objspace'
=> true
[2] pry(main)> ObjectSpace.memsize_of('')
=> 40
[3] pry(main)> ObjectSpace.memsize_of([])
=> 40
[4] pry(main)> ObjectSpace.memsize_of(1..100)
=> 40
[5] pry(main)> ObjectSpace.memsize_of('X' * 100)
=> 141
[6] pry(main)> ObjectSpace.memsize_of(('X' * 100).chars)
=> 840
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With