Behind the Code: Stacks and Heaps - Page 2
       by kirupa  |  29 March 2007

In the previous page, you were learning about stacks and how a simple program gets stored on the stack. In this page, I will provide some extra background info, wrap up the discussion on stacks, and introduce you to heaps!

Recursion
If I were talking about a lower level language, then I would have to elaborate more on safe coding practices. For example, a recursive function can inflate the stack very quickly because the stack will end up storing the return location for each recursive function call. The solution is to modify your function to work iteratively. In an iterative solution, you are directly modifying the result of a previous function call instead of storing a collection of results from previous function call and saving any calculations until the end. Luckily, such optimizations are done automatically for us, and you can learn more about that by learning more about tail-recursion [wikipedia]. Let's meet Heap!

Heaps
Heaps, like mentioned in the previous page, deal primarily with reference types, and one popular reference type that you may already be familiar with is a class. To best explain how the heap works, let me first show you an example:

class Simpsons
{
public string name;
public int age;
}
 
public void People()
{
Simpsons bart = new Simpsons();
bart.name = "Bart";
bart.age = 10;
}

The above code has a class called Simpsons, and the Simpsons class has two public fields called name and age. You access the Simpsons class via your People method, and inside your People method, you both create a bart object of type Simspons and initialize the bart object's name and age fields.

Now that you an idea of what the code does, let's take a look at how both our stack and heap look like after running the People method:

Your stack, like before, contains the People method call, and it includes an entry for bart. Notice that bart isn't pointing to a value, but instead it is referencing the Simpsons object on the heap. The reason is that Simpsons is a class aka a reference type, and like I mentioned before, reference types usually belong on the heap.

Garbage Collection
After your People method has finished running, your stack would once again be empty since it only contains information on code that is or will be running. With nothing left to run, our stack will be empty. The heap, on the other hand, can still contain data:

In such a situation, you will have something called a Garbage Collector that goes through and clears unreferenced/unused data from the heap. For example, the Simpsons class above would be removed because it is no longer being used, and you will end up with an empty stack and heap. I should clarify that in most scenarios, when your application is more complicated than the example presented in this post, you may have some data from other methods taking up space in both your stack and heap.

Onwards to the next page!

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.