Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It might help to clarify what platform's GC you are asking about - JVM, CLR, Lisp, etc. That said:</p> <p>First to take a step back, certain local variables of are generally allocated on the stack. The specifics can vary by language, however. To take C# as an example, only local <a href="http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx" rel="noreferrer">Value Types</a> and method parameters are stored on the stack. So, in C#, <code>foo</code> would be allocated on the stack:</p> <pre><code>public function bar() { int foo = 2; ... } </code></pre> <p>Alternatively, dynamically-allocated variables use memory from the heap. This should intuitively make sense, as otherwise the stack would have to grow dynamically each time a <code>new</code> is called. Also, it would mean that such variables could only be used as locals within the local function that allocated them, which is of course not true because we can have (for example) class member variables. So to take another example from C#, in the following case <code>result</code> is allocated on the heap:</p> <pre><code>public class MyInt { public int MyValue; } ... MyInt result = new MyInt(); result.MyValue = foo + 40; ... </code></pre> <p>Now with that background in mind, memory on the <b>heap</b> is garbage-collected. Memory on the stack has no need for GC as the memory will be reclaimed when the current function returns. At a high level, a GC algorithm works by keeping track of all objects that are dynamically allocated on the heap. Once allocated via <code>new</code>, the object will be tracked by GC, and collected when it is no longer in scope and there are no more references to it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload