Blog,  Learn Code

C#: Stack vs. Heap

Lets talk about memory. Specifically how a computer program stores information so that it can be accessed.

Programs and application store values that are used to perform their purpose. Sometimes the values are coming from the user, other times the values are pre-set in the application itself. In either event They are often set in something called a variable. A variable is really just a way for naming and keeping track of a particular value. And as we consider variables and what a variable is meant to represent, we have to look at the “type” of value it is storing. In previous posts we discussed variables and types. Today we want to discuss the two storage types: Heap and Stack.

Stack Memory

There are certain types that are stored in what is called the Stack. You can think of the stack as a stack of building blocks. Each time you create a VALUE typed variable, it’s value is stored on the stack. As a reminder (because we are talking about memory, right?) the types that are classified as VALUE type are: bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, uint, ulong, ushort – each of these types has a known size, so it is easy for the application and computer to know how much storage each variable needs. You may hear that this allocation of memory is static, meaning it is not going to change. If you use a type of int it had defined range that it can occupy, even if you do not use it all.

But these types are short term, in the sense that they are actively stored on the STACK while they are in scope. As an example, if you see a block of code like the following:

//...other code above

if(x > 0)
{
    var sum = x + x;
}

//...more code

Clearly this is an extremely simplified block, and really serves no value, but for our illustration. Let’s ask: Can you use the value of sum outside the last curly bracket? No. Why not? Because it has gone out of scope. It is declared and lives only inside the confines of those curly brackets. Once the progression moves outside the closing bracket, it signals to the program that it is not needed anymore and that memory can be deallocated or freed up for use by other needs.

Value types are also stored directly on the stack. If the image below were our stack, each variable has it’s own allocated space, and the value of the variable is stored in that allocated space.

One thing to watch for is that if you have an application that loops recursively, you can quickly fill up all the available stack memory. If this happens you may see an error or an exception called ” StackOverflowException”.

Heap Memory

Heap memory is for Reference types – those are classes, interfaces, delegates, objects, and strings. These type of objects are not fixed defined sizes (aka they are not static), they are dynamic.

Heap storage is slower that stack storage. But Heap types do not go out of scope the way stack types do. When we closed out curly bracket, it meant that the Value type variables inside the block were cleared away and no longer available to the program. Things stored on the Heap are available to all the threads of the application. They are not closed automatically. That is one point to remember – you as the programmer need to close them out if you are done with them.

Let’s consider this example:

Here we create two string variables. The first is given the assigned value of “Welcome!”, and the second is given the value of the first. We see that the actual value of “Welcome!” is stored on the heap. However, the variables that we created also show up in the stack, not with the string value, but with a reference telling them where to find the value on the Heap. And they both have the value to the same block of storage on the heap.

Hopefully that helps in understanding the differences between things stored on the stack versus things stored on the heap. Thanks for reading – and feel free to let us know in the comments section if there are aspects that you want to know about.