title: How do modern VMs handle memory allocation?
body: <p>I'm working on a simple stack machine written in C, mostly for learning purposes. After using <code>malloc/free</code> for my memory operations, I thought it would be a good idea to read some memory allocation specific code from modern virtual machines.</p>
  
  <p>I downloaded Lua source code and started reading it. After a while, I realized there are lots of macro stuff involved, and I couldn't find the code where real memory allocation is done (ie. <code>malloc</code> call).</p>
  
  <pre><code>find . -exec grep -i "malloc" '{}' \; -print
  </code></pre>
  
  <p>It printed only some Lua macros that have <code>malloc</code> word in their names. The Lua VM (and programming language) doesn't use <code>malloc</code> at all!</p>
  
  <p>So this leads me to the question: how do modern VMs handle memory allocation? How does Lua allocate memory from the heap? Are there any ways for allocation other than <code>malloc</code>? What are the pros/cons of other methods?</p>
  
  <p>I'm also wondering about best-practices, design-patterns, etc. for safely working on allocated memory. I see in Lua's source that there is lots of indirection before allocating memory. Where can I learn about this stuff?</p>