Welcome back.
So in this video I want to talk about something a bit more related to the core of C sharp, as in how
C sharp handles what is being made and deleted.
So what do I mean?
Well you know how we've been creating new things all the time, like the new structs from before and
how we can remove, delete and even create temporary variables that are outside its scope.
So it would just be deleted.
Right.
So what happens to these?
Luckily, C-sharp is a language that pretty much handles it by itself with no action from our side.
But let's learn a bit about how it does that.
All right so let's get started.
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 plus, plus 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 sharp.
That's why it's not something that yeah, many people use very often.
So once you have more complex programs and are 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 you even have to use it 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 reuse in the
system once objects are no longer being used.
So in 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 an 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 sees, 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 free it 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 dense object in the memory is not required anymore because the function call is
over.
So the function ends the object, then it goes out of scope and there are no references to Denis 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 would just clean up this Dennis and reallocate 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.
Or the garbage collector, even if you call it manually, doesn't have to run straight away.
So these are some tricky things about the garbage collector.
You can implement the Finalizer 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 Finalizer method
within this class in order to implement a 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.
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 Gc.collect method is called.
Even though this is a super tricky method.
So this is something that is yeah, it's very specific, very special.
If you want to know more about it, you should check out the documentation on the garbage collector.
But the 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 know, 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.
Okay, so I hope that this was easy to understand.
Luckily, as I said, you should not have to worry about memory management as C sharp already handles
that, but it's also good to know how it handles it.
So yeah, that's it for this video.
See you in the next one.