I am wondering how memory allocation works in Ruby.
In Ruby, we can open a class definition and add more instance variables/methods. In this case, new objects would require more memory than existing instances. How does it internally track memory allocated to an instance?
For example, we have the following class definition:
class MyClass
def myMethod
@a = 5
end
end
We created an instance of MyClass
:
m = MyClass.new
m.myMethod
At this point, m
would be allocated some memory. Now, we open the class definition and add another instance variable b
.
class MyClass
def mySecondMethod
@b = 5
end
end
This new method mySecondMethod
and instance variable b
are also available in the existing instance m
of Myclass
.
Calling mySecondMethod
to instantiate and initialize b
:
m.mySecondMethod
How does Ruby internally manage memory? Does it reallocate the memory chunk for instance m
, or does it maintain some sort of pointers to the newly allocated memory?
The answer depends on the actual implementation. Here I assume you are asking about MRI.
Ruby objects are allocated on the heap. There is no concept of the stack when talking about object allocations.
The heap is split up into pages, each consisting of 16kb. Each page is carved up into fixed size slots which can hold Ruby objects. A page can hold ~408 objects, since each object (which is an RVALUE
struct) occupies 40bytes.
All of this is managed by the VM (ie. YARV).
source: http://timetobleed.com/garbage-collection-slides-from-la-ruby-conference/
Regarding your example, variables just hold references to objects, so m
actually points to an allocated MyClass
object.
The C struct (RClass
) that backs up MyClass
internally, contains a pointer to a table with the user-defined methods like #mySecondMethod
and a pointer to a table with the names of the instance variables that its objects have.
Each object (which is backed up by RObject
since the Object
class is the default root of all objects) internally contains a pointer to the values of its instance variables.
The newly defined #mySecondMethod
is available because of the dynamic nature of the language and the fact that method lookup happens at runtime.
Your second guess is correct. Ruby objects maintain pointers to heap memory.
Note, however, that in your example, because mySecondMethod
is never called on your object m
, that instance variable @b
is not set/initialized for m
.
The thing that will probably help you most is, first, to get a little bit more practice using Ruby and understanding how its class and object idioms work. Then, I would suggest looking at some resources like this very good and recent article about the Ruby garbage collector.
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