title: Object memory allocation
body: <p>I am wondering how memory allocation works in Ruby.</p>
  
  <p>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?</p>
  
  <p>For example, we have the following class definition:</p>
  
  <pre><code>class MyClass
      def myMethod
          @a = 5
      end
  end
  </code></pre>
  
  <p>We created an instance of <code>MyClass</code>:</p>
  
  <pre><code>m = MyClass.new
  m.myMethod
  </code></pre>
  
  <p>At this point, <code>m</code> would be allocated some memory. Now, we open the class definition and add another instance variable <code>b</code>.</p>
  
  <pre><code>class MyClass
      def mySecondMethod
          @b = 5
      end
  end
  </code></pre>
  
  <p>This new method <code>mySecondMethod</code> and instance variable <code>b</code> are also available in the existing instance <code>m</code> of <code>Myclass</code>.</p>
  
  <p>Calling <code>mySecondMethod</code> to instantiate and initialize <code>b</code>:</p>
  
  <pre><code>m.mySecondMethod
  </code></pre>
  
  <p>How does Ruby internally manage memory? Does it reallocate the memory chunk for instance <code>m</code>, or does it maintain some sort of pointers to the newly allocated memory?</p>