There is no language function that calculates the size of a class in the same way as C.
The memory size of an object is implementation dependent. It depends on the implementation of the base class object. It is also not easy to evaluate the memory used. For example, strings can be embedded in an RString structure if they are short, but stored on the heap if they are long ( Never create Ruby strings longer than 23 characters ).
The memory taken by some objects was tabulated for different ruby implementations: Object memory in Ruby 1.8, EE, 1.9 and OCaml
Finally, the size of an object can differ even with two objects from the same class, since you can optionally add additional instance variables without hard coding which instance variables are present. For example, see instance_variable_get and instance_variable_set
If you use MRI ruby 1.9.2+, there is a method that you can try (be warned that it only looks at part of the object, this is obvious from the fact that integers and strings are of zero size)
irb(main):176:0> require 'objspace' => true irb(main):176:0> ObjectSpace.memsize_of(134) => 0 irb(main):177:0> ObjectSpace.memsize_of("asdf") => 0 irb(main):178:0> ObjectSpace.memsize_of({a: 4}) => 184 irb(main):179:0> ObjectSpace.memsize_of({a: 4, b: 5}) => 232 irb(main):180:0> ObjectSpace.memsize_of(/a/.match("a")) => 80
You can also try memsize_of_all (note that it looks at the memory usage of the entire interpreter, and overwriting the variable does not appear to immediately delete the old copy):
irb(main):190:0> ObjectSpace.memsize_of_all => 4190347 irb(main):191:0> asdf = 4 => 4 irb(main):192:0> ObjectSpace.memsize_of_all => 4201350 irb(main):193:0> asdf = 4 => 4 irb(main):194:0> ObjectSpace.memsize_of_all => 4212353 irb(main):195:0> asdf = 4.5 => 4.5 irb(main):196:0> ObjectSpace.memsize_of_all => 4223596 irb(main):197:0> asdf = "a" => "a" irb(main):198:0> ObjectSpace.memsize_of_all => 4234879
You have to be very careful because there is no guarantee when the Ruby interpreter will perform garbage collection. Although you can use it for testing and experimentation, it is recommended that this is NOT used in production!