← Back
IAmTimCorey September 7, 2026 26m

Garbage Collection in C#

Read full transcript 20 segments
  1. Memory management in C is really easy on Memory management in C is really easy on us developers, but do you know how it us developers, but do you know how it us developers, but do you know how it actually works? In this video, I'm going actually works? In this video, I'm going actually works? In this video, I'm going to walk you through how garbage to walk you through how garbage to walk you through how garbage collection works in C. By the end, you collection works in C. By the end, you collection works in C. By the end, you should have a better knowledge of how to should have a better knowledge of how to should have a better knowledge of how to allocate objects and why you should not allocate objects and why you should not allocate objects and why you should not touch the garbage collection system. touch the garbage collection system. touch the garbage collection system. Now, if you don't know me, my name is Now, if you don't know me, my name is Now, if you don't know me, my name is Tim Corey and I help developers grow in Tim Corey and I help developers grow in Tim Corey and I help developers grow in their career, whether they're just just their career, whether they're just just their career, whether they're just just getting started or have been developers getting started or have been developers getting started or have been developers for years now. If you're looking for for years now. If you're looking for for years now. If you're looking for even more software development training, even more software development training, even more software development training, head over to devforge.com where I have head over to devforge.com where I have head over to devforge.com where I have dozens of courses designed to make you a dozens of courses designed to make you a dozens of courses designed to make you a better developer. Plus, I have a whole better developer. Plus, I have a whole better developer. Plus, I have a whole section dedicated to more free section dedicated to more free section dedicated to more free resources. All right, let's jump into resources. All right, let's jump into resources. All right, let's jump into how to work with garbage collection in how to work with garbage collection in how to work with garbage collection in C. Last week, we talked about the C. Last week, we talked about the C. Last week, we talked about the difference of stack versus heap. We're difference of stack versus heap. We're difference of stack versus heap. We're going to kind of briefly touch on that, going to kind of briefly touch on that, going to kind of briefly touch on that, but if you want more information about but if you want more information about but if you want more information about stack versus heap and how that all stack versus heap and how that all stack versus heap and how that all works, go watch the previous video from works, go watch the previous video from works, go watch the previous video from last week. By the way, I put out a video last week. By the way, I put out a video last week. By the way, I put out a video every Monday that's dedicated to C sharp every Monday that's dedicated to C sharp every Monday that's dedicated to C sharp training or the related systems. So training or the related systems. So training or the related systems. So definitely make sure that you're definitely make sure that you're definitely make sure that you're subscribed to the channel because that's subscribed to the channel because that's subscribed to the channel because that's how you get notified of new videos as how you get notified of new videos as how you get notified of new videos as they come out. Okay, so the stack they come out. Okay, so the stack they come out. Okay, so the stack memory, how it works just so we have a memory, how it works just so we have a memory, how it works just so we have a baseline here is that memory gets put baseline here is that memory gets put baseline here is that memory gets put into what's called a frame. And so into what's called a frame. And so into what's called a frame. And so method one, let's say has an int, a method one, let's say has an int, a method one, let's say has an int, a bool, a double, another int. That gets bool, a double, another int. That gets bool, a double, another int. That gets put into a frame. And method two has an put into a frame. And method two has an put into a frame. And method two has an int and a bool. Method three has a int and a bool. Method three has a int and a bool. Method three has a double datetime instruct. Well, if

  2. double datetime instruct. Well, if double datetime instruct. Well, if method three completes, then this frame method three completes, then this frame method three completes, then this frame just gets deleted from memory. That's just gets deleted from memory. That's just gets deleted from memory. That's what happen. It is gone. And then the what happen. It is gone. And then the what happen. It is gone. And then the same things happen with that method two, same things happen with that method two, same things happen with that method two, method one. They just get deleted when method one. They just get deleted when method one. They just get deleted when they're no longer needed. That's how the they're no longer needed. That's how the they're no longer needed. That's how the um the frames work is very easy to um the frames work is very easy to um the frames work is very easy to deallocate or just just get rid of a deallocate or just just get rid of a deallocate or just just get rid of a frame of memory. But that's not how the frame of memory. But that's not how the frame of memory. But that's not how the heap works. So let's look at the heap heap works. So let's look at the heap heap works. So let's look at the heap and why it's a bit messier. and why it's a bit messier. and why it's a bit messier. Let's look at this kind of quick diagram Let's look at this kind of quick diagram Let's look at this kind of quick diagram of what it might look like in your of what it might look like in your of what it might look like in your application where you have this person application where you have this person application where you have this person model P. Classes A, B, and C all use an model P. Classes A, B, and C all use an model P. Classes A, B, and C all use an instance of that person model. So instance of that person model. So instance of that person model. So they're all referencing P. And so they they're all referencing P. And so they they're all referencing P. And so they all have a reference to this object. all have a reference to this object. all have a reference to this object. That object is stored once in memory on That object is stored once in memory on That object is stored once in memory on the heap, but all three different places the heap, but all three different places the heap, but all three different places have access to that. And actually have access to that. And actually have access to that. And actually program.cs CS has reference to class A program.cs CS has reference to class A program.cs CS has reference to class A to class B to class C. So all this to class B to class C. So all this to class B to class C. So all this intertwined references means that we intertwined references means that we intertwined references means that we can't just say oh well program you know can't just say oh well program you know can't just say oh well program you know person model P is done. Why is it done?

  3. person model P is done. Why is it done? person model P is done. Why is it done? How do you know it's done? Because maybe How do you know it's done? Because maybe How do you know it's done? Because maybe class A is done with person model P but class A is done with person model P but class A is done with person model P but class B and class C aren't. This is why class B and class C aren't. This is why class B and class C aren't. This is why it's very difficult to just say oh now it's very difficult to just say oh now it's very difficult to just say oh now is when we can get rid of P. And this is is when we can get rid of P. And this is is when we can get rid of P. And this is where the garbage collection system where the garbage collection system where the garbage collection system comes in. comes in. comes in. So at a high level, we're going to go So at a high level, we're going to go So at a high level, we're going to go into more detail, but at a high level, into more detail, but at a high level, into more detail, but at a high level, what happens is it waits and it says, what happens is it waits and it says, what happens is it waits and it says, okay, let's say that program.cs is done okay, let's say that program.cs is done okay, let's say that program.cs is done with class A. Class A is done. So we get with class A. Class A is done. So we get with class A. Class A is done. So we get rid of this this connection here, right? rid of this this connection here, right? rid of this this connection here, right? So now class A is no longer being So now class A is no longer being So now class A is no longer being referenced anywhere. So it can be referenced anywhere. So it can be referenced anywhere. So it can be destroyed. It can get we can get rid of destroyed. It can get we can get rid of destroyed. It can get we can get rid of that memory usage. actually it's still that memory usage. actually it's still that memory usage. actually it's still reference to person model P but because reference to person model P but because reference to person model P but because class A is not being referenced anymore class A is not being referenced anymore class A is not being referenced anymore doesn't really matter. So at some point doesn't really matter. So at some point doesn't really matter. So at some point the garbage collector will come along the garbage collector will come along the garbage collector will come along and say oh class A is not needed anymore and say oh class A is not needed anymore and say oh class A is not needed anymore I can go ahead and get rid of that and I can go ahead and get rid of that and I can go ahead and get rid of that and when I do that it also gets rid of that when I do that it also gets rid of that when I do that it also gets rid of that reference to the person model P. So now reference to the person model P. So now reference to the person model P. So now class A is gone out of memory because class A is gone out of memory because class A is gone out of memory because the garbage collector came along and the garbage collector came along and the garbage collector came along and said oh you don't need that anymore. The said oh you don't need that anymore. The said oh you don't need that anymore. The garbage collector came along too and garbage collector came along too and garbage collector came along too and said, "Wait, you still need B and you said, "Wait, you still need B and you said, "Wait, you still need B and you still need C and you still need person still need C and you still need person still need C and you still need person model P." So it knows about those and model P." So it knows about those and model P." So it knows about those and maybe class C doesn't gets rid of the maybe class C doesn't gets rid of the maybe class C doesn't gets rid of the reference to person model P. Maybe it's reference to person model P. Maybe it's reference to person model P. Maybe it's it's out outside of a scope or something it's out outside of a scope or something it's out outside of a scope or something like that. So now C is still in use. B like that. So now C is still in use. B like that. So now C is still in use. B is still in use and person model P is is still in use and person model P is is still in use and person model P is still in use. So nothing gets collected.

  4. still in use. So nothing gets collected. still in use. So nothing gets collected. But at some point maybe B says, "You But at some point maybe B says, "You But at some point maybe B says, "You know what? We're done with we get rid of know what? We're done with we get rid of know what? We're done with we get rid of that variable that's holding the that variable that's holding the that variable that's holding the reference to P." Well, now P is off on reference to P." Well, now P is off on reference to P." Well, now P is off on its own. And again, the garbage its own. And again, the garbage its own. And again, the garbage collector will come along and say, "Oh, collector will come along and say, "Oh, collector will come along and say, "Oh, I can get rid of this memory." And it I can get rid of this memory." And it I can get rid of this memory." And it cleans it up. But how does it do that? cleans it up. But how does it do that? cleans it up. But how does it do that? How does it know when to do that? How How does it know when to do that? How How does it know when to do that? How does it identify which things to get rid does it identify which things to get rid does it identify which things to get rid of? And how does it do so efficiently? of? And how does it do so efficiently? of? And how does it do so efficiently? Because this is not an easy process. you Because this is not an easy process. you Because this is not an easy process. you have to walk through this entire tree. have to walk through this entire tree. have to walk through this entire tree. So, let's back up really briefly here. So, let's back up really briefly here. So, let's back up really briefly here. We have this whole tree here that's very We have this whole tree here that's very We have this whole tree here that's very very simple for a real application, but very simple for a real application, but very simple for a real application, but yet the garbage collector has to figure yet the garbage collector has to figure yet the garbage collector has to figure out, okay, what is program.cs reference? out, okay, what is program.cs reference? out, okay, what is program.cs reference? Oh, it references A, B, and C. What does Oh, it references A, B, and C. What does Oh, it references A, B, and C. What does A reference? Well, it references P. What A reference? Well, it references P. What A reference? Well, it references P. What does B reference? Well, it references P. does B reference? Well, it references P. does B reference? Well, it references P. What does C reference? Oh, it repres What does C reference? Oh, it repres What does C reference? Oh, it repres references P. That's a very simple graph references P. That's a very simple graph references P. That's a very simple graph and yet there's a lot to traverse and and yet there's a lot to traverse and and yet there's a lot to traverse and figure out and what's connected, what's figure out and what's connected, what's figure out and what's connected, what's not connected and and you know if a if not connected and and you know if a if not connected and and you know if a if this arrow gets deleted versus this this arrow gets deleted versus this this arrow gets deleted versus this arrow gets deleted, what happens and so arrow gets deleted, what happens and so arrow gets deleted, what happens and so on. There's a lot there. The garbage on. There's a lot there. The garbage on. There's a lot there. The garbage collector handles that for us. Let's get collector handles that for us. Let's get collector handles that for us. Let's get into more details on how. So let's start into more details on how. So let's start into more details on how. So let's start with generations.

  5. with generations. with generations. So what happens is when you allocate an So what happens is when you allocate an So what happens is when you allocate an object and then the garbage collector object and then the garbage collector object and then the garbage collector and we'll talk about when it runs in a and we'll talk about when it runs in a and we'll talk about when it runs in a minute but the garbage collector runs minute but the garbage collector runs minute but the garbage collector runs and it says hey I want to do some and it says hey I want to do some and it says hey I want to do some cleanup. Think of it as your Roomba of cleanup. Think of it as your Roomba of cleanup. Think of it as your Roomba of your net application. It comes along and your net application. It comes along and your net application. It comes along and says let's go ahead and clean the says let's go ahead and clean the says let's go ahead and clean the memory. What it's going to do is it's memory. What it's going to do is it's memory. What it's going to do is it's going to look at all the new objects going to look at all the new objects going to look at all the new objects which the new objects typically we'll which the new objects typically we'll which the new objects typically we'll talk about the exception in a little talk about the exception in a little talk about the exception in a little while but typically the new objects get while but typically the new objects get while but typically the new objects get put into what's called generation zero put into what's called generation zero put into what's called generation zero objects. So these are the the newly objects. So these are the the newly objects. So these are the the newly created objects. So let's say you you created objects. So let's say you you created objects. So let's say you you create a method you create 5,000 new create a method you create 5,000 new create a method you create 5,000 new objects but you only use them in that objects but you only use them in that objects but you only use them in that method briefly and then the method method briefly and then the method method briefly and then the method closes. You're done with that. So closes. You're done with that. So closes. You're done with that. So they're short-lived objects. Probably they're short-lived objects. Probably they're short-lived objects. Probably the garbage collector will have only run the garbage collector will have only run the garbage collector will have only run maybe once but probably even zero times. maybe once but probably even zero times. maybe once but probably even zero times. It probably hadn't run yet before you're It probably hadn't run yet before you're It probably hadn't run yet before you're done referencing those objects. So what done referencing those objects. So what done referencing those objects. So what happens is garbage collection zero gets happens is garbage collection zero gets happens is garbage collection zero gets run. So the garbage collector runs in run. So the garbage collector runs in run. So the garbage collector runs in three different uh generations. So at three different uh generations. So at three different uh generations. So at this point we're going to say the this point we're going to say the this point we're going to say the garbage collector is running on garbage collector is running on garbage collector is running on generation zero. It says, "Okay, all the generation zero. It says, "Okay, all the generation zero. It says, "Okay, all the new objects that are flagged as new objects that are flagged as new objects that are flagged as generation zero, which they get flagged generation zero, which they get flagged generation zero, which they get flagged that way by default at the beginning, it that way by default at the beginning, it that way by default at the beginning, it says, hey, are there references to all says, hey, are there references to all says, hey, are there references to all of these generation zero objects?"

  6. of these generation zero objects?" of these generation zero objects?" Basically, are they still being used? Basically, are they still being used? Basically, are they still being used? And it finds that class A, it says, "Oh, And it finds that class A, it says, "Oh, And it finds that class A, it says, "Oh, it's not being used anymore." It could it's not being used anymore." It could it's not being used anymore." It could get rid of it right away. But maybe it get rid of it right away. But maybe it get rid of it right away. But maybe it says, "Oh, no, class A is still being says, "Oh, no, class A is still being says, "Oh, no, class A is still being used." Then most likely, and there's used." Then most likely, and there's used." Then most likely, and there's there's exceptions to this rule, but there's exceptions to this rule, but there's exceptions to this rule, but most likely what happens is if class A most likely what happens is if class A most likely what happens is if class A is being used, when the garbage is being used, when the garbage is being used, when the garbage collector does a collection and it says, collector does a collection and it says, collector does a collection and it says, "Oh, class A is still being used," then "Oh, class A is still being used," then "Oh, class A is still being used," then it promotes it and it promotes it to it promotes it and it promotes it to it promotes it and it promotes it to generation one or gen one. And what that generation one or gen one. And what that generation one or gen one. And what that does is remember the garbage collector does is remember the garbage collector does is remember the garbage collector right now is running on generation zero. right now is running on generation zero. right now is running on generation zero. This is the fastest generation to run on This is the fastest generation to run on This is the fastest generation to run on because it's the the short-lived objects because it's the the short-lived objects because it's the the short-lived objects that are probably easy to clean up. And that are probably easy to clean up. And that are probably easy to clean up. And so it probably cleans up most of the so it probably cleans up most of the so it probably cleans up most of the objects that it finds in generation objects that it finds in generation objects that it finds in generation zero. The ones that it finds that are zero. The ones that it finds that are zero. The ones that it finds that are are not able to be cleaned up, it moves are not able to be cleaned up, it moves are not able to be cleaned up, it moves up one level to generation one. So it up one level to generation one. So it up one level to generation one. So it moves class A at generation one. Well, moves class A at generation one. Well, moves class A at generation one. Well, now the next time it runs, it might run now the next time it runs, it might run now the next time it runs, it might run again at generation zero, looking for a again at generation zero, looking for a again at generation zero, looking for a new object to clean up. And it might not new object to clean up. And it might not new object to clean up. And it might not run on generation one for a little bit.

  7. run on generation one for a little bit. run on generation one for a little bit. And here's why. because generation zero And here's why. because generation zero And here's why. because generation zero is super fast to do. We'll talk more is super fast to do. We'll talk more is super fast to do. We'll talk more about why in a little bit, but basically about why in a little bit, but basically about why in a little bit, but basically what it is is because of the fact that what it is is because of the fact that what it is is because of the fact that these are newly created objects, most of these are newly created objects, most of these are newly created objects, most of them usually get destroyed within a them usually get destroyed within a them usually get destroyed within a short period of time. Therefore, it's short period of time. Therefore, it's short period of time. Therefore, it's very easy to clean up most of the very easy to clean up most of the very easy to clean up most of the memory. And when the garbage collector memory. And when the garbage collector memory. And when the garbage collector cleans up the memory, it doesn't just cleans up the memory, it doesn't just cleans up the memory, it doesn't just delete objects. It actually compacts the delete objects. It actually compacts the delete objects. It actually compacts the memory as well and then reassigns the memory as well and then reassigns the memory as well and then reassigns the references. We'll see how that's done in references. We'll see how that's done in references. We'll see how that's done in a little bit. But when that happens, a little bit. But when that happens, a little bit. But when that happens, it's very easy when most of the stuff it's very easy when most of the stuff it's very easy when most of the stuff gets deleted. Okay. So, and what it gets deleted. Okay. So, and what it gets deleted. Okay. So, and what it does, it has what's called a budget. So, does, it has what's called a budget. So, does, it has what's called a budget. So, just like your your home spending just like your your home spending just like your your home spending budget, only unlike your home spending budget, only unlike your home spending budget, only unlike your home spending budget, this budget kind of adjusts over budget, this budget kind of adjusts over budget, this budget kind of adjusts over time dynamically. It figures out what it time dynamically. It figures out what it time dynamically. It figures out what it should have in this budget for objects should have in this budget for objects should have in this budget for objects and at some point may say, "Hey, you and at some point may say, "Hey, you and at some point may say, "Hey, you know what? Generation one is exceeding know what? Generation one is exceeding know what? Generation one is exceeding its budget. We have three budgets. One its budget. We have three budgets. One its budget. We have three budgets. One for gen 0, one for gen one, one for gen for gen 0, one for gen one, one for gen for gen 0, one for gen one, one for gen two. So even though it may be able to two. So even though it may be able to two. So even though it may be able to clean more out of gen 1, it goes, hey, clean more out of gen 1, it goes, hey, clean more out of gen 1, it goes, hey, you know what, gen I'm sorry, of gen you know what, gen I'm sorry, of gen you know what, gen I'm sorry, of gen zero, it may say, you know what, gen one zero, it may say, you know what, gen one zero, it may say, you know what, gen one is exceeding its budget for memory is exceeding its budget for memory is exceeding its budget for memory allocations. Remember that that we're allocations. Remember that that we're allocations. Remember that that we're talking about memory here. This is just talking about memory here. This is just talking about memory here. This is just memory inmemory usage. So at some point, memory inmemory usage. So at some point, memory inmemory usage. So at some point, it's going to say, okay, I need to go it's going to say, okay, I need to go it's going to say, okay, I need to go clean up gen one. And when it does, it's clean up gen one. And when it does, it's clean up gen one. And when it does, it's going to look at all objects that are in going to look at all objects that are in going to look at all objects that are in generation one as well as generation

  8. generation one as well as generation generation one as well as generation zero. It's going to do both of those. So zero. It's going to do both of those. So zero. It's going to do both of those. So let's say that the garbage collector let's say that the garbage collector let's say that the garbage collector comes along and looks at the generation comes along and looks at the generation comes along and looks at the generation one uh memory allocations and it says, one uh memory allocations and it says, one uh memory allocations and it says, "Hey, class one or class A, are you in "Hey, class one or class A, are you in "Hey, class one or class A, are you in use?" And if it's not in use, it gets use?" And if it's not in use, it gets use?" And if it's not in use, it gets rid of it and compacts that memory. But rid of it and compacts that memory. But rid of it and compacts that memory. But let's say it is in use still. Then what let's say it is in use still. Then what let's say it is in use still. Then what it does usually, again, there's slight it does usually, again, there's slight it does usually, again, there's slight caveats to when this could happen, but caveats to when this could happen, but caveats to when this could happen, but in general, what happens is if class A in general, what happens is if class A in general, what happens is if class A is still in use, when the collection is still in use, when the collection is still in use, when the collection runs in generation one, then it says, runs in generation one, then it says, runs in generation one, then it says, "Hey, you know what, class A, we're "Hey, you know what, class A, we're "Hey, you know what, class A, we're going to promote you again to Gen 2. Now going to promote you again to Gen 2. Now going to promote you again to Gen 2. Now you're Gen 2 object." you're Gen 2 object." you're Gen 2 object." And why is this a big deal? Well, most And why is this a big deal? Well, most And why is this a big deal? Well, most of our longrunning objects live in Gen of our longrunning objects live in Gen of our longrunning objects live in Gen 2. This is where most of the memory gets 2. This is where most of the memory gets 2. This is where most of the memory gets used for our application. This is for used for our application. This is for used for our application. This is for caching and other objects that live for caching and other objects that live for caching and other objects that live for a very long time live in this space. So a very long time live in this space. So a very long time live in this space. So this is the actually the most expensive this is the actually the most expensive this is the actually the most expensive generation to clean up. First of all, generation to clean up. First of all, generation to clean up. First of all, because you're check cleaning all three because you're check cleaning all three because you're check cleaning all three generations, but also because there's so generations, but also because there's so generations, but also because there's so much stuff in this memory and because of much stuff in this memory and because of much stuff in this memory and because of the fact that you're then looking at the fact that you're then looking at the fact that you're then looking at every single object in your application every single object in your application every single object in your application instead of just the new ones or the new instead of just the new ones or the new instead of just the new ones or the new and semi-new ones. And so by looking at and semi-new ones. And so by looking at and semi-new ones. And so by looking at all of them, it takes a lot more effort, all of them, it takes a lot more effort, all of them, it takes a lot more effort, a lot more processing power to trace a lot more processing power to trace a lot more processing power to trace through every route for every object to

  9. through every route for every object to through every route for every object to see are you still in use or not? Because see are you still in use or not? Because see are you still in use or not? Because remember back to our diagram here, it remember back to our diagram here, it remember back to our diagram here, it has to trace these lines through to say, has to trace these lines through to say, has to trace these lines through to say, okay, person model P, are you in use? okay, person model P, are you in use? okay, person model P, are you in use? Well, it's got to trace to figure out, Well, it's got to trace to figure out, Well, it's got to trace to figure out, oh, it's in use here. It's in use here oh, it's in use here. It's in use here oh, it's in use here. It's in use here to figure out if it's in use or not. So, to figure out if it's in use or not. So, to figure out if it's in use or not. So, it's it's a complicated process and it's it's a complicated process and it's it's a complicated process and there are lots of optimizations, by the there are lots of optimizations, by the there are lots of optimizations, by the way, lots of optimizations in how this way, lots of optimizations in how this way, lots of optimizations in how this works. But just note that once it gets works. But just note that once it gets works. But just note that once it gets into generation 2, that's the most into generation 2, that's the most into generation 2, that's the most expensive memory to clean up. So this is expensive memory to clean up. So this is expensive memory to clean up. So this is why you want your objects to be as why you want your objects to be as why you want your objects to be as short-lived as possible because if they short-lived as possible because if they short-lived as possible because if they can if all possible get cleaned up with can if all possible get cleaned up with can if all possible get cleaned up with generation zero, it's very very cheap. generation zero, it's very very cheap. generation zero, it's very very cheap. Which means the same object in C in Which means the same object in C in Which means the same object in C in memory can be expensive or cheap. It all memory can be expensive or cheap. It all memory can be expensive or cheap. It all depends on how long it lives because if depends on how long it lives because if depends on how long it lives because if you let it live only a short period of you let it live only a short period of you let it live only a short period of time and get clean up a gen zero cleanup time and get clean up a gen zero cleanup time and get clean up a gen zero cleanup then it's a lot cheaper than if you let then it's a lot cheaper than if you let then it's a lot cheaper than if you let it bubble up to gen two. Now this is not it bubble up to gen two. Now this is not it bubble up to gen two. Now this is not prescription to say well then never have prescription to say well then never have prescription to say well then never have anything live long term. That's anything live long term. That's anything live long term. That's obviously not practical. We still have obviously not practical. We still have obviously not practical. We still have to live inside of our our practicality to live inside of our our practicality to live inside of our our practicality here of we're building an application.

  10. here of we're building an application. here of we're building an application. But when you're thinking about working But when you're thinking about working But when you're thinking about working with objects, you should think about, with objects, you should think about, with objects, you should think about, hey, does this need to live longer or hey, does this need to live longer or hey, does this need to live longer or could this be something I only let live could this be something I only let live could this be something I only let live for a short period of time. So, it's for a short period of time. So, it's for a short period of time. So, it's important to think these things through. important to think these things through. important to think these things through. So, that's how the generations work. So, that's how the generations work. So, that's how the generations work. Let's keep going. Talk about memory. Let's keep going. Talk about memory. Let's keep going. Talk about memory. So, here's the process for how the So, here's the process for how the So, here's the process for how the garbage collector works. It first garbage collector works. It first garbage collector works. It first identifies dead objects. Objects that identifies dead objects. Objects that identifies dead objects. Objects that are no longer being referenced. Then are no longer being referenced. Then are no longer being referenced. Then what it does is it deletes them. And what it does is it deletes them. And what it does is it deletes them. And then number two, it compacts the memory. then number two, it compacts the memory. then number two, it compacts the memory. Number three, it updates the memory Number three, it updates the memory Number three, it updates the memory addresses. Let's see a practical example addresses. Let's see a practical example addresses. Let's see a practical example of this. I have five classes here. These of this. I have five classes here. These of this. I have five classes here. These are the madeup memory locations for are the madeup memory locations for are the madeup memory locations for these five objects. Okay, so let's just these five objects. Okay, so let's just these five objects. Okay, so let's just say that class number two, this is say that class number two, this is say that class number two, this is actually no longer in use. And class actually no longer in use. And class actually no longer in use. And class number four, same thing. No longer in number four, same thing. No longer in number four, same thing. No longer in use. So the first thing that the garbage use. So the first thing that the garbage use. So the first thing that the garbage collect does, identify that and get rid collect does, identify that and get rid collect does, identify that and get rid of these two objects. So now they're of these two objects. So now they're of these two objects. So now they're gone. But there's gaps here in the gone. But there's gaps here in the gone. But there's gaps here in the memory. And remember that on the heap, memory. And remember that on the heap, memory. And remember that on the heap, objects are different sizes. So when you objects are different sizes. So when you objects are different sizes. So when you have an integer, it's a fixed size. A have an integer, it's a fixed size. A have an integer, it's a fixed size. A double is a fixed size. But when you're double is a fixed size. But when you're double is a fixed size. But when you're talking about a a string or a class talking about a a string or a class talking about a a string or a class object, these are heap items that could object, these are heap items that could object, these are heap items that could be you know a few bytes or they could be be you know a few bytes or they could be be you know a few bytes or they could be very very large like megabytes of size.

  11. very very large like megabytes of size. very very large like megabytes of size. So therefore how much memory it used is So therefore how much memory it used is So therefore how much memory it used is variable. So what happens after the variable. So what happens after the variable. So what happens after the first step of getting rid of dead first step of getting rid of dead first step of getting rid of dead objects is that the garbage collector objects is that the garbage collector objects is that the garbage collector compacts the memory. So what it does is compacts the memory. So what it does is compacts the memory. So what it does is it moves class three over here and it it moves class three over here and it it moves class three over here and it moves class five down here and now we moves class five down here and now we moves class five down here and now we have all this memory this free memory at have all this memory this free memory at have all this memory this free memory at the end. Therefore, if a big object the end. Therefore, if a big object the end. Therefore, if a big object comes in, let's just say we bring a new comes in, let's just say we bring a new comes in, let's just say we bring a new object in and this object is well, object in and this object is well, object in and this object is well, let's um try this again. Um ungroup it let's um try this again. Um ungroup it let's um try this again. Um ungroup it and and and we'll make this object larger, right? we'll make this object larger, right? we'll make this object larger, right? So, it can take up, you know, two So, it can take up, you know, two So, it can take up, you know, two sections of memory instead of one. Okay, sections of memory instead of one. Okay, sections of memory instead of one. Okay, that that new one. So that's why it that that new one. So that's why it that that new one. So that's why it compacts it. So it has room to do this compacts it. So it has room to do this compacts it. So it has room to do this instead of saying, "Oh, I got a whole instead of saying, "Oh, I got a whole instead of saying, "Oh, I got a whole bunch of gaps that none are big enough bunch of gaps that none are big enough bunch of gaps that none are big enough for this object." So therefore, we can for this object." So therefore, we can for this object." So therefore, we can get rid of this. Um, but therefore, it get rid of this. Um, but therefore, it get rid of this. Um, but therefore, it compacts the memory down. But here's the compacts the memory down. But here's the compacts the memory down. But here's the problem. Let's say class one referenced problem. Let's say class one referenced problem. Let's say class one referenced class 3, but it referenced it at memory class 3, but it referenced it at memory class 3, but it referenced it at memory position 3000, but that's not where it's position 3000, but that's not where it's position 3000, but that's not where it's at anymore. That's that's class 5 now.

  12. at anymore. That's that's class 5 now. at anymore. That's that's class 5 now. So what the next step is is update the So what the next step is is update the So what the next step is is update the memory addresses. So now it says, "Oh, memory addresses. So now it says, "Oh, memory addresses. So now it says, "Oh, anywhere that reference class 3, that's anywhere that reference class 3, that's anywhere that reference class 3, that's now memory address 2,000. Anywhere that now memory address 2,000. Anywhere that now memory address 2,000. Anywhere that reference class 5, that's now memory reference class 5, that's now memory reference class 5, that's now memory address 30,000." So it kind of it address 30,000." So it kind of it address 30,000." So it kind of it shuffles those things around. Now, we shuffles those things around. Now, we shuffles those things around. Now, we don't see that in our code or in the don't see that in our code or in the don't see that in our code or in the actual running application because of actual running application because of actual running application because of the fact that we're a higher level the fact that we're a higher level the fact that we're a higher level language. We don't see how the memory is language. We don't see how the memory is language. We don't see how the memory is actually being allocated. This is one of actually being allocated. This is one of actually being allocated. This is one of the benefits of having a higher level the benefits of having a higher level the benefits of having a higher level language. We don't need to care language. We don't need to care language. We don't need to care necessarily. So we just know what's necessarily. So we just know what's necessarily. So we just know what's happening but we don't have to keep happening but we don't have to keep happening but we don't have to keep track of the memory registers and what's track of the memory registers and what's track of the memory registers and what's full and what's not etc. So this is the full and what's not etc. So this is the full and what's not etc. So this is the steps of the garbage collector. Now, if steps of the garbage collector. Now, if steps of the garbage collector. Now, if you think about dealing with, let's say you think about dealing with, let's say you think about dealing with, let's say you are in gen zero, lots of lots of you are in gen zero, lots of lots of you are in gen zero, lots of lots of gaps, very few items left, easy compact, gaps, very few items left, easy compact, gaps, very few items left, easy compact, you're only moving a couple things and you're only moving a couple things and you're only moving a couple things and changing a couple references. Imagine changing a couple references. Imagine changing a couple references. Imagine being in Gen 2 where you talk about being in Gen 2 where you talk about being in Gen 2 where you talk about basically all the memory and the fact basically all the memory and the fact basically all the memory and the fact that you got a lot of objects that are that you got a lot of objects that are that you got a lot of objects that are still alive which means there's a lot of still alive which means there's a lot of still alive which means there's a lot of moving around for compacting but then a moving around for compacting but then a moving around for compacting but then a lot of updates to references. There's a lot of updates to references. There's a lot of updates to references. There's a lot of work to be done once you get into lot of work to be done once you get into lot of work to be done once you get into moving lots of objects that stay around moving lots of objects that stay around moving lots of objects that stay around after this is complete. That's why it after this is complete. That's why it after this is complete. That's why it gets more and more expensive gen zero to gets more and more expensive gen zero to gets more and more expensive gen zero to gen one to gen two. So this is why with

  13. gen one to gen two. So this is why with gen one to gen two. So this is why with garbage collection, we really want stuff garbage collection, we really want stuff garbage collection, we really want stuff to go away in gen zero if it's to go away in gen zero if it's to go away in gen zero if it's practical. practical. practical. Okay, so that's how the garbage Okay, so that's how the garbage Okay, so that's how the garbage collector works, how it cleans things collector works, how it cleans things collector works, how it cleans things up, how it compacts things down. So up, how it compacts things down. So up, how it compacts things down. So let's talk about some questions. Um, let's talk about some questions. Um, let's talk about some questions. Um, when does the garbage collector run? when does the garbage collector run? when does the garbage collector run? That's a great question. um the garbage That's a great question. um the garbage That's a great question. um the garbage collector runs under a couple of collector runs under a couple of collector runs under a couple of different circumstances. One, there's different circumstances. One, there's different circumstances. One, there's some allocation pressure. This is like some allocation pressure. This is like some allocation pressure. This is like um if you are I if you allocate let's 10 um if you are I if you allocate let's 10 um if you are I if you allocate let's 10 10,000 objects, there's a lot of objects 10,000 objects, there's a lot of objects 10,000 objects, there's a lot of objects that have been recently allocated. So it that have been recently allocated. So it that have been recently allocated. So it says, hey, you know what? There's been a says, hey, you know what? There's been a says, hey, you know what? There's been a lot of stuff allocated recently. Let's lot of stuff allocated recently. Let's lot of stuff allocated recently. Let's go ahead and clean that up. So that's go ahead and clean that up. So that's go ahead and clean that up. So that's one time. Um, another time is if you one time. Um, another time is if you one time. Um, another time is if you have memory pressure. Maybe you you go have memory pressure. Maybe you you go have memory pressure. Maybe you you go from using a 100 megabytes to a gigabyte from using a 100 megabytes to a gigabyte from using a 100 megabytes to a gigabyte of memory. It goes, hey, you know what? of memory. It goes, hey, you know what? of memory. It goes, hey, you know what? Let's just go ahead and check that to Let's just go ahead and check that to Let's just go ahead and check that to see if we can't bring some of that down.

  14. see if we can't bring some of that down. see if we can't bring some of that down. And then the third time is when you have And then the third time is when you have And then the third time is when you have a low memory condition where, let's say a low memory condition where, let's say a low memory condition where, let's say you have 4 GB of RAM in your system and you have 4 GB of RAM in your system and you have 4 GB of RAM in your system and your system is down to 500 megabytes and your system is down to 500 megabytes and your system is down to 500 megabytes and goes, "Oh, you know what? We're getting goes, "Oh, you know what? We're getting goes, "Oh, you know what? We're getting close on memory here. Let's go ahead and close on memory here. Let's go ahead and close on memory here. Let's go ahead and um compact some things down and and free um compact some things down and and free um compact some things down and and free up some memory. So that's when the up some memory. So that's when the up some memory. So that's when the garbage collector runs. There's garbage collector runs. There's garbage collector runs. There's different time. It's not on a schedule. different time. It's not on a schedule. different time. It's not on a schedule. It's not every x seconds. It's not It's not every x seconds. It's not It's not every x seconds. It's not every, you know, once certain things every, you know, once certain things every, you know, once certain things happen. Those are the the big times when happen. Those are the the big times when happen. Those are the the big times when the garbage collector runs. There is one the garbage collector runs. There is one the garbage collector runs. There is one more time when it runs more time when it runs more time when it runs and that's when users do it on their and that's when users do it on their and that's when users do it on their own. Okay. So, own. Okay. So, own. Okay. So, don't do that. Okay. And we're going to don't do that. Okay. And we're going to don't do that. Okay. And we're going to cover that in point four, but that's the cover that in point four, but that's the cover that in point four, but that's the that's the other time when it happens. that's the other time when it happens. that's the other time when it happens. Don't do that. We're going to talk about Don't do that. We're going to talk about Don't do that. We're going to talk about why. Okay. Number two. Do all objects why. Okay. Number two. Do all objects why. Okay. Number two. Do all objects start at Gen Z. Um, no. No. Not all start at Gen Z. Um, no. No. Not all start at Gen Z. Um, no. No. Not all objects start at Gen Z. Some objects objects start at Gen Z. Some objects objects start at Gen Z. Some objects start at Gen 2. Which ones? The ones start at Gen 2. Which ones? The ones start at Gen 2. Which ones? The ones where they go into the uh large I'm look where they go into the uh large I'm look where they go into the uh large I'm look my notes here. The large object heap.

  15. my notes here. The large object heap. my notes here. The large object heap. It's a LOH. Um, another acronym for you, It's a LOH. Um, another acronym for you, It's a LOH. Um, another acronym for you, large object heap. So, the large object large object heap. So, the large object large object heap. So, the large object heap is for large objects. It's for when heap is for large objects. It's for when heap is for large objects. It's for when you're allocating very large things in you're allocating very large things in you're allocating very large things in memory. And so, it immediately puts it memory. And so, it immediately puts it memory. And so, it immediately puts it into Gen 2. So, if your object takes up into Gen 2. So, if your object takes up into Gen 2. So, if your object takes up more than 85,000 more than 85,000 more than 85,000 bytes of memory, it puts it into the bytes of memory, it puts it into the bytes of memory, it puts it into the large object heap. Okay, [clears throat] large object heap. Okay, [clears throat] large object heap. Okay, [clears throat] so that's kind of the one exception, the so that's kind of the one exception, the so that's kind of the one exception, the the big exception for when you get a gen the big exception for when you get a gen the big exception for when you get a gen two immediately as opposed to gen zero. two immediately as opposed to gen zero. two immediately as opposed to gen zero. Um why are collecting gen one and gen Um why are collecting gen one and gen Um why are collecting gen one and gen two more expensive? Um we kind of two more expensive? Um we kind of two more expensive? Um we kind of covered that, but gen one again more covered that, but gen one again more covered that, but gen one again more objects going to live longer. So objects going to live longer. So objects going to live longer. So therefore we got more to compact, more therefore we got more to compact, more therefore we got more to compact, more to move around, more references. And gen to move around, more references. And gen to move around, more references. And gen two is all the things essentially. And two is all the things essentially. And two is all the things essentially. And so we have a lot of moving and shuffling so we have a lot of moving and shuffling so we have a lot of moving and shuffling things around for a little bit of memory things around for a little bit of memory things around for a little bit of memory s savings, but that means a whole bunch s savings, but that means a whole bunch s savings, but that means a whole bunch of references are changing because of of references are changing because of of references are changing because of that. So um very expensive. So we try that. So um very expensive. So we try that. So um very expensive. So we try really hard to not move things at gen really hard to not move things at gen really hard to not move things at gen not allow things to move at gen two that not allow things to move at gen two that not allow things to move at gen two that don't need to. And again we don't don't need to. And again we don't don't need to. And again we don't control this through saying don't move control this through saying don't move control this through saying don't move to gen two. We control this by saying, to gen two. We control this by saying, to gen two. We control this by saying, "Hey, I'm gonna allocate something in a "Hey, I'm gonna allocate something in a "Hey, I'm gonna allocate something in a loop, but I'm gonna make sure those loop, but I'm gonna make sure those loop, but I'm gonna make sure those those references don't live outside the those references don't live outside the those references don't live outside the loop if they don't have to. So that's loop if they don't have to. So that's loop if they don't have to. So that's how we we kind of control some of that.

  16. how we we kind of control some of that. how we we kind of control some of that. And then that this brings us to the big And then that this brings us to the big And then that this brings us to the big question uh about garbage collection, question uh about garbage collection, question uh about garbage collection, which is why should we not trigger a which is why should we not trigger a which is why should we not trigger a garbage collection event ourselves?" garbage collection event ourselves?" garbage collection event ourselves?" Some people think that there's there's a Some people think that there's there's a Some people think that there's there's a there is code in C to trigger a garbage there is code in C to trigger a garbage there is code in C to trigger a garbage collection event. I thought about doing collection event. I thought about doing collection event. I thought about doing a demo of this, but I didn't find any a demo of this, but I didn't find any a demo of this, but I didn't find any value in it. In fact, I found a little value in it. In fact, I found a little value in it. In fact, I found a little bit of danger in it because it kind of bit of danger in it because it kind of bit of danger in it because it kind of encouraged you to think, "Oh, I could do encouraged you to think, "Oh, I could do encouraged you to think, "Oh, I could do that." Don't do that. So, we're just not that." Don't do that. So, we're just not that." Don't do that. So, we're just not going to do a demo. We're going to talk going to do a demo. We're going to talk going to do a demo. We're going to talk about it. So, why should you not trigger about it. So, why should you not trigger about it. So, why should you not trigger a a garbage collection event? Because a a garbage collection event? Because a a garbage collection event? Because the thought is, well, if I trigger that the thought is, well, if I trigger that the thought is, well, if I trigger that garbage collection event, it's going to garbage collection event, it's going to garbage collection event, it's going to clean out memory, and that I don't want clean out memory, and that I don't want clean out memory, and that I don't want to have memory leaks. This has nothing to have memory leaks. This has nothing to have memory leaks. This has nothing to do with memory leaks. Garbage to do with memory leaks. Garbage to do with memory leaks. Garbage collection does not deal with memory collection does not deal with memory collection does not deal with memory leaks. A memory leak is when you have leaks. A memory leak is when you have leaks. A memory leak is when you have memory that's allocated and never memory that's allocated and never memory that's allocated and never unallocated. And when would that be? unallocated. And when would that be? unallocated. And when would that be? It's when you're not releasing a It's when you're not releasing a It's when you're not releasing a reference to it. If you keep a reference reference to it. If you keep a reference reference to it. If you keep a reference to it, the garbage collector cannot to it, the garbage collector cannot to it, the garbage collector cannot collect it. Therefore, you just keep collect it. Therefore, you just keep collect it. Therefore, you just keep using more and more and more memory.

  17. using more and more and more memory. using more and more and more memory. That's not what garbage collection deals That's not what garbage collection deals That's not what garbage collection deals with. So then a question is, well, but with. So then a question is, well, but with. So then a question is, well, but my app's using a lot of memory and I my app's using a lot of memory and I my app's using a lot of memory and I want to make sure it uses less memory. want to make sure it uses less memory. want to make sure it uses less memory. Then allocate less objects. Don't ask Then allocate less objects. Don't ask Then allocate less objects. Don't ask the garbage collector to do more cleanup the garbage collector to do more cleanup the garbage collector to do more cleanup because what's going to happen? Let's because what's going to happen? Let's because what's going to happen? Let's move back up to this demonstration right move back up to this demonstration right move back up to this demonstration right here. Let's say you have a short-lived here. Let's say you have a short-lived here. Let's say you have a short-lived class, but it's in CLA, it's in class, but it's in CLA, it's in class, but it's in CLA, it's in generation zero, but you trigger the generation zero, but you trigger the generation zero, but you trigger the garbage collector manually. Well, that garbage collector manually. Well, that garbage collector manually. Well, that may prematurely move this to Gen 1 or may prematurely move this to Gen 1 or may prematurely move this to Gen 1 or even Gen 2. What you're doing is moving even Gen 2. What you're doing is moving even Gen 2. What you're doing is moving more things higher up the generation more things higher up the generation more things higher up the generation list than they otherwise naturally would list than they otherwise naturally would list than they otherwise naturally would have been, which means you make it more have been, which means you make it more have been, which means you make it more expensive to clean these up, which means expensive to clean these up, which means expensive to clean these up, which means you're going to make your system run you're going to make your system run you're going to make your system run slower. You're going to take up more slower. You're going to take up more slower. You're going to take up more memory for longer, and you're generally memory for longer, and you're generally memory for longer, and you're generally going to make your application worse going to make your application worse going to make your application worse because of the fact that you're trying because of the fact that you're trying because of the fact that you're trying to manually step in and say, "Yes, but to manually step in and say, "Yes, but to manually step in and say, "Yes, but garbage collector run now." So, just garbage collector run now." So, just garbage collector run now." So, just don't do that.

  18. don't do that. don't do that. There's probably a super super rare There's probably a super super rare There's probably a super super rare exception, but if you come in the exception, but if you come in the exception, but if you come in the comments and say, "I definitely have comments and say, "I definitely have comments and say, "I definitely have exception." I I'm not going to believe exception." I I'm not going to believe exception." I I'm not going to believe you. That's that's what's going to come you. That's that's what's going to come you. That's that's what's going to come down to. Um because I have seen a lot of down to. Um because I have seen a lot of down to. Um because I have seen a lot of people tell me, "I have the exception." people tell me, "I have the exception." people tell me, "I have the exception." There's a reason why we run the garbage There's a reason why we run the garbage There's a reason why we run the garbage collection. collection. collection. You're probably causing more harm than You're probably causing more harm than You're probably causing more harm than good. That's what it comes down to. Um, good. That's what it comes down to. Um, good. That's what it comes down to. Um, so just know that garbage collection so just know that garbage collection so just know that garbage collection exists. Know how the generations work exists. Know how the generations work exists. Know how the generations work and how you want to keep things and how you want to keep things and how you want to keep things short-lived if possible to stay in this short-lived if possible to stay in this short-lived if possible to stay in this gen zero or even gen one era where they gen zero or even gen one era where they gen zero or even gen one era where they live for a short period of time. They live for a short period of time. They live for a short period of time. They get cleaned up more cheaply and you move get cleaned up more cheaply and you move get cleaned up more cheaply and you move on. Try very hard to do that. Focus on on. Try very hard to do that. Focus on on. Try very hard to do that. Focus on making sure that you are right, you making sure that you are right, you making sure that you are right, you know, not creating objects you don't know, not creating objects you don't know, not creating objects you don't need to create. don't create massive need to create. don't create massive need to create. don't create massive objects just to store them in memory. objects just to store them in memory. objects just to store them in memory. Um, so many other things you can do to Um, so many other things you can do to Um, so many other things you can do to make your application more efficient, make your application more efficient, make your application more efficient, but none of them deal with actually but none of them deal with actually but none of them deal with actually directly implementing or or managing the directly implementing or or managing the directly implementing or or managing the garbage collection. Now, I have touched garbage collection. Now, I have touched garbage collection. Now, I have touched on the bare minimum of what the garbage on the bare minimum of what the garbage on the bare minimum of what the garbage collector does. And if you think, wow, collector does. And if you think, wow, collector does. And if you think, wow, that's simple. I can do that. This this that's simple. I can do that. This this that's simple. I can do that. This this is 1% of what actually goes on behind is 1% of what actually goes on behind is 1% of what actually goes on behind the scenes because we haven't talked the scenes because we haven't talked the scenes because we haven't talked about the fact that back here in in this about the fact that back here in in this about the fact that back here in in this diagram maybe the class A is a a gen 2 diagram maybe the class A is a a gen 2 diagram maybe the class A is a a gen 2 object but maybe it allocates a gen zero

  19. object but maybe it allocates a gen zero object but maybe it allocates a gen zero object. If you do a gen zero garbage object. If you do a gen zero garbage object. If you do a gen zero garbage collection, how does it know to check collection, how does it know to check collection, how does it know to check the references and it's only checking in the references and it's only checking in the references and it's only checking in Gen Z? Well, there's a card system that Gen Z? Well, there's a card system that Gen Z? Well, there's a card system that it has that it it tracks those things it has that it it tracks those things it has that it it tracks those things without being too expensive to go without being too expensive to go without being too expensive to go looking through all the different looking through all the different looking through all the different objects. There's a lot that goes on objects. There's a lot that goes on objects. There's a lot that goes on beyond the complexity of what I've beyond the complexity of what I've beyond the complexity of what I've explained. This is the the simple explained. This is the the simple explained. This is the the simple overview of garbage collection. There's overview of garbage collection. There's overview of garbage collection. There's a lot more to it. There's a lot more a lot more to it. There's a lot more a lot more to it. There's a lot more depth to how the garbage collector depth to how the garbage collector depth to how the garbage collector works. The budgeting, like I said, it's works. The budgeting, like I said, it's works. The budgeting, like I said, it's a sliding scale. It moves the budgeting a sliding scale. It moves the budgeting a sliding scale. It moves the budgeting around based upon a number of different around based upon a number of different around based upon a number of different circumstances. So, it's not like it just circumstances. So, it's not like it just circumstances. So, it's not like it just says, well, I've got a 100 megabytes in says, well, I've got a 100 megabytes in says, well, I've got a 100 megabytes in gen zero and a thousand in gen one and gen zero and a thousand in gen one and gen zero and a thousand in gen one and the rest in gen two. It's not how it the rest in gen two. It's not how it the rest in gen two. It's not how it does that. What it does is based upon does that. What it does is based upon does that. What it does is based upon how it's working and how what's being how it's working and how what's being how it's working and how what's being allocated, what's being saved through allocated, what's being saved through allocated, what's being saved through the generations and it adjusts the the generations and it adjusts the the generations and it adjusts the budget accordingly. There's a lot of budget accordingly. There's a lot of budget accordingly. There's a lot of stuff that goes on beyond what we've stuff that goes on beyond what we've stuff that goes on beyond what we've talked about that makes it much much talked about that makes it much much talked about that makes it much much much more complex. But the cool thing is much more complex. But the cool thing is much more complex. But the cool thing is we don't have to do that. And this is we don't have to do that. And this is we don't have to do that. And this is where again a highlevel language may where again a highlevel language may where again a highlevel language may cost you just a little bit in cost you just a little bit in cost you just a little bit in performance.

  20. performance. performance. So we're not at bare metal here. We're So we're not at bare metal here. We're So we're not at bare metal here. We're not talking to the memory ourselves. not talking to the memory ourselves. not talking to the memory ourselves. We're letting do that for us. But We're letting do that for us. But We're letting do that for us. But because we are, we're not having to do because we are, we're not having to do because we are, we're not having to do all of this work tracking down and all of this work tracking down and all of this work tracking down and figuring out, hey, are are you uh free figuring out, hey, are are you uh free figuring out, hey, are are you uh free for allocation for, you know, for allocation for, you know, for allocation for, you know, deallocation? And if you are then can I deallocation? And if you are then can I deallocation? And if you are then can I you know compact the memory and how do I you know compact the memory and how do I you know compact the memory and how do I re reress the the memory and and all the re reress the the memory and and all the re reress the the memory and and all the we don't have to do that it's done for we don't have to do that it's done for we don't have to do that it's done for us it's super efficient and we don't us it's super efficient and we don't us it's super efficient and we don't have to think about it so I encourage have to think about it so I encourage have to think about it so I encourage you know that it's there know that you know that it's there know that you know that it's there know that garbage collection exists know how it garbage collection exists know how it garbage collection exists know how it works in general at a high level works in general at a high level works in general at a high level understand how to be more efficient as a understand how to be more efficient as a understand how to be more efficient as a developer because of the garbage developer because of the garbage developer because of the garbage collector but don't actually start collector but don't actually start collector but don't actually start manipulating the garbage collector manipulating the garbage collector manipulating the garbage collector yourself. Okay, so that's garbage yourself. Okay, so that's garbage yourself. Okay, so that's garbage collection in C. Thanks for watching and collection in C. Thanks for watching and collection in C. Thanks for watching and as always I am Tim Corey. [music]

Summary

This tech transcript explains how garbage collection works in C#, contrasting it with stack memory. It emphasizes that while stack memory is automatically deallocated, heap memory requires careful management. The key takeaway is to understand object allocation on the heap and to avoid directly manipulating the garbage collection system.

View original episode ↗