Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find memory used by ruby object?

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?

like image 840
Sandip Karanjekar Avatar asked Dec 10 '12 13:12

Sandip Karanjekar


1 Answers

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
like image 192
Travis Avatar answered Sep 21 '22 17:09

Travis