‫We'll come back.
‫In this video, I would like to talk about the garbage collector and how C-sharp takes care of memory.
‫So the garbage collection is a tool that is available in multiple programming languages.
‫And in some of them it's done manually and others it's done automatically.
‫And the dot net framework provides an automatic memory management, which means that you don't have
‫to use the garbage collector manually as you do in all the programming languages.
‫So programming languages such as C or C++ and others, they have a manual garbage collection that you
‫have to run every now and then and you have to know when you have to run it.
‫So that's something that is done automatically in C shop.
‫That's why it's not something that yeah, many people use very often.
‫So once you have more complex programs and really advanced, then you will start using it every now
‫and then.
‫But really it's not something that you have to use all the time or even have to use at all in most cases.
‫So that's why it's not a major topic, so to speak.
‫All right.
‫So let's say we have this object called Dennis and it's of type human.
‫So I created a class which is called human and there are multiple different properties and so forth.
‫So in the end, I'm just creating an object of type human, and that's called Dennis.
‫And by doing so, by using this new keyword, I require memory because it creates a object in my memory.
‫So C Sharp allocates memory for us and the framework takes care of reclaiming memory for use in the
‫system once objects are no longer being used.
‫So this case, I'm creating this object and then I might use it and I might use it again and again.
‫But at one point I'm not going to use it anymore and I'm maybe even cutting the references to it.
‫And then, yeah, the garbage collector will clean it up because this memory is now blocked and the
‫garbage collector has to clean up.
‫So it has to make this memory available again for other objects, for example.
‫Now let's look at it from a visual perspective.
‫So we have this system memory pool on the left hand side and our program on the right hand side.
‫And the system memory pool has a bunch of memory, so we can use this memory for multiple different
‫programs.
‫But now we have our program which runs it, and now in our program we create an object.
‫Now, suddenly we need a little bit of space in our memory.
‫All right.
‫So there's a reference from our program to that memory where our object is stored.
‫All right.
‫So there's a direct reference and we can reuse it and we can call it from different spots and so forth.
‫All right.
‫And then we have another one and another one.
‫So we have multiple different objects that we created.
‫And each time we create a new object, there is a memory allocated in the memory pool and there is a
‫reference from our program to that memory.
‫So these references are there.
‫So there's a reference from our program to the memory pool.
‫Now at one point the reference might be deleted.
‫So we maybe delete the reference by setting a variable null or by setting the object null, and then
‫the reference is suddenly gone.
‫Now there is no reference from our program to this little memory allocation here anymore.
‫So our garbage collector notices it and says, okay, there's no reference, so this little piece of
‫memory is never used, so let's just clean it up or let's re allocate it or let's just freed up for
‫other allocation.
‫And that's what happens then.
‫So now let's have a look at an example of when the garbage collector would kick in.
‫And I have a function here called my function, and I'm creating an object here.
‫Again, it's called Dennis.
‫And then Dennis is calling his methods, or we call his methods such as teach.
‫Then we set his age and then we call another method, get older and so forth.
‫But at one point, this Dennis object in the memory is not required anymore because the function call
‫is over.
‫So the function ends the object.
‫Dennis goes out of scope and there are no references to Dennis anymore because my function is a function
‫and we have this object which is within a function, so the scope of it is within the function.
‫So once the function call is over, there's no direct reference to it anymore because we can't reference
‫to an object which is within a function from outside of this function.
‫Right.
‫So we would need to create fields for that or do other approaches for this.
‫But in the end, the garbage collector, which is clean up this Dennis and re-allocate this memory.
‫And the general idea here is really that this is just one simple example.
‫There are plenty of other situations where the garbage collector would kick in, but this is something
‫that we have already used.
‫So the concepts that you can see here, we have seen them in other videos or lectures of this course
‫already.
‫That's why I chose this example.
‫So what do you need to know about the garbage collection?
‫Well, you don't have to call the garbage collector manually, but you can, so you can do it manually.
‫The garbage collector requires processing power, so it's on a separate thread and it's very lightweight.
‫But still some processing power is required.
‫The garbage collectors goal is to free up memory but not use too much processing power.
‫So it will not need much processing power in general, but it's not going to be called all the time
‫or the garbage collector won't run all the time to free up memory.
‫It's just going to run when it's necessary or when it deems necessary, when it thinks it's necessary.
‫So that's really the tricky part here.
‫The memory isn't necessarily reclaimed right away.
‫It might take a little while for the garbage collector, even if you call it manually, doesn't have
‫to run straightaway.
‫So these are some tricky things about the garbage collector.
‫You can implement a finalized method to execute code just before an object is released from memory by
‫the garbage collector.
‫This means that if you decide to run a specific code just before an object is cleaned up, so just before
‫the garbage collector is cleaning it up.
‫So once there are no references to it anymore, for example, then you can use the final laser method
‫within this class in order to implement the specific functionality that you want them to run.
‫Maybe you want to clean up stuff yourself just before the garbage collector comes in and cleans up your
‫object or relocates the memory or releases it to this object from memory.
‫All right.
‫When does the garbage collector run?
‫Well, when the system has low physical memory, when the memory allocated exceeds a preset threshold,
‫when the garbage collector collect or collect method is called, even though this is a super tricky
‫method.
‫So this is something that is.
‫Yes, very specific, very special.
‫If you want to know more about it, you should check out the documentation on the garbage collector.
‫But the dot collect method isn't always called straight away and it doesn't directly clean everything
‫up.
‫It's more like you're giving the tool a suggestion, so you're saying, okay, garbage collector.
‫I personally think that would be a good point or a good spot to collect the garbage, but in some cases
‫it might not accept it or it might just do it later on or something like that.
‫Maybe it just did garbage collection and it decides that it's not necessary or something like that.
‫So it's a very tricky one.
‫If you want to know more about it, check out the documentation.
‫But I think this is a good starting point to understand what the garbage collector does.
‫And as I said, in most cases you won't need it because nowadays we have so much memory.
‫And if you don't have huge memory leaks in your programs, you should never run into these problems.