Is there a method to see the size of allocated memory for a class in ruby?
I have built a custom class and I would like to know its size in memory. So is there a function with the likeness of sizeof()
in C?
I am simply trying to initialize a new class like so
test = MyClass.new
and trying to find a method to print out the size of the class that has been allocated to memory.
Is this even possible in ruby?
Class size refers to the number of students in a given course or classroom, specifically either (1) the number of students being taught by individual teachers in a course or classroom or (2) the average number of students being taught by teachers in a school, district, or education system.
In C++, Size of empty structure/class will be one byte as because to call function at least empty structure should have some size ( minimum 1 byte is required ) i.e. one byte.
The pupil-teacher ratio (PTR), or the number of students at a site or in a classroom divided by the total number of educators who serve the site or classroom, is 30 to 1. If the classroom has 30 students and two teachers, then the class size is still 30—but the pupil-teacher ratio is now 15 to 1.
To know the size of the class you can just calculate the variable size of each data variable used inside the class. This will be equal to the size of the object initialised.
There is no language feature 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 simple to estimate the memory used. For example, strings can be embedded in an RString structure if they are short, but stored in the heap if they are long (Never create Ruby strings longer than 23 characters).
The memory taken by some objects has been tabulated for different ruby implementations: Memory footprint of objects in Ruby 1.8, EE, 1.9, and OCaml
Finally, the object size may differ even with two objects from the same class, since it is possible to arbitrarily add extra instance variables, without hardcoding what 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 you can try (be warned that it is looking at only part of the object, this is obvious from the fact that integers and strings appear to have 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 whole interpreter, and overwriting a variable does not appear to delete the old copy immediately):
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 should be very careful because there is no guarantee when the Ruby interpreter will perform garbage collection. While you might use this for testing and experimentation, it is recommended that this is NOT used in production!
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