Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object memory allocation

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?

like image 970
Sanket Meghani Avatar asked Feb 18 '14 05:02

Sanket Meghani


2 Answers

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).

ruby heap layoutsource: 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.

like image 145
14 revs Avatar answered Sep 18 '22 12:09

14 revs


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.

like image 25
user513951 Avatar answered Sep 19 '22 12:09

user513951