mrbgems/mruby-os-memsize/README.md
The mruby-os-memsize mrbgem extends the ObjectSpace module in mruby, providing tools to inspect the approximate amount of heap memory allocated for objects. This can be useful for debugging memory usage and understanding the memory footprint of your mruby application.
This gem adds two class methods to the ObjectSpace module:
ObjectSpace.memsize_of(obj) -> NumericReturns the approximate amount of heap memory allocated for the given obj in bytes.
size_t type of the underlying C implementation.Example:
str = "This is a test string"
array = [1, 2, 3, 4, 5]
puts "Size of string: #{ObjectSpace.memsize_of(str)} bytes"
puts "Size of array: #{ObjectSpace.memsize_of(array)} bytes"
puts "Size of 123: #{ObjectSpace.memsize_of(123)}" # Likely 0
puts "Size of :symbol: #{ObjectSpace.memsize_of(:symbol)}" # Likely 0
class MyClass
def initialize
@data = "some internal data"
end
end
instance = MyClass.new
puts "Size of MyClass instance: #{ObjectSpace.memsize_of(instance)}"
ObjectSpace.memsize_of_all([klass]) -> NumericReturns the total approximate heap memory allocated for all living objects in the mruby environment.
klass argument (a Class object) is provided, it returns the total memory size only for instances of that specific class.Example:
# Get total memory size of all objects
total_memory = ObjectSpace.memsize_of_all
puts "Total heap memory used by all objects: #{total_memory} bytes"
# Get total memory size for all String objects
total_string_memory = ObjectSpace.memsize_of_all(String)
puts "Total heap memory used by Strings: #{total_string_memory} bytes"
class Person
def initialize(name)
@name = name
end
end
p1 = Person.new("Alice")
p2 = Person.new("Bob")
total_person_memory = ObjectSpace.memsize_of_all(Person)
puts "Total heap memory used by Person instances: #{total_person_memory} bytes"
This mrbgem is released under the MIT License. (See mrbgem.rake for details within the mruby distribution).
mruby developers