[00:00] Today we're going to have a quick look at stack and heap memory when your application is running. memory can be really helpful to work out why things have different scopes. [00:15] The first part is machine code. is converted into instructions that your computer can understand. Now to understand the stack, you need to understand the stack data structure. [00:31] so you imagine you've got a book stacked like this. and you can put things on the top of the stack, So we can't take anything for the middle, we can't take it to the bottom. [00:46] All we can do is just add stuff to the top, take it off, in that order. Now the call stack has a couple of responsibilities. returned to after the current method is finished executing. [01:02] that's going to get added to the call stack and each subsequent Now the second thing that the call stack is responsible for is for keeping track of the local variables in your method. [01:16] Now the heap, unlike the stack, allows you to store items in memory in any order. Now, with this added ability, [01:29] and therefore adding items to the heap has a higher overheads Now generally the heap is used whenever you have data So if you have a variable that needs to be accessed across different methods [01:45] then that is going to live on the heap. where your variables are stored in memory. Now value types just store the value. [02:01] So generally data types such as this, such as INT will be stored to a value. So we have two parts to a reference type. We have a pointer which is generally just an address to a location in memory. [02:16] For value types, it really depends on where you declare it. we know that we can't access that variable from outside the method. that is because that local variable is going to get stored on the call [02:31] So soon as execution is finished, that block on the call isn't going to be available anymore. Now, if we were to declare a reference type variable inside your method, [02:44] The value for your reference type variable will be on the heap, once the method execution is finished. It really depends on where you declare it in your code. [03:00] because you need to be able to access it from different areas in your code. And for example, if you create a class and then have variables inside your class, is also going to live on the heap and so the variables inside it. [03:15] Now because our reference types only have a pointer to a location in memory, what happens to that block of memory after your method is finished executing? Now, as the name suggests, the garbage collector goes around collecting garbage. [03:30] It goes around and looks at the heap and finds things aren't being used anymore So this is how when a reference type thats declared in a method is no longer the garbage collector comes, it cleans it up. [03:44] Value types are either on the stack or the heap, depending on where there declared. Static variables, for example, will always be on the heap [03:57] Now, in some languages such as C#, we have what we call anonymous functions. but they can be created and called from inside another method. [04:10] that you declared in the method that called it. those variables need to at least temporarily be stored on the heap, Of course, as soon as you call that anonymous [04:26] and therefore not have access to anything that is on the previous level down. we're going to have a bunch of data that's just going to be dangling around Now, the other exception is asynchronous methods. [04:42] anything asynchronously, it's going to get run on a different thread. it has multiple threads and each thread has its own call stack. is independent of one another, they can finish independently as well [04:55] and therefore be able to access the results of your asynchronous methods. so when you call your asynchronous method, Your current thread is going to continue [05:07] And all of those results are just going to get stored If you like this video, please make sure you like and subscribe and you might want to check out this one on Bitwise Operators and why we use them. [05:23] Thank you for watching. I'll see you in the next video.