Hello.
In this video, we'll talk about topic nine, what our generation's garbage collector has a lot of work.
It needs to identify objects to be removed, which we learned about in the previous lecture.
Then it must actually remove them.
Finally, it must fragment the memory of the application.
As we know, the garbage collector marks objects as writable or unreachable and removes the latter from
their memory.
Imagine there is some object.
Let's call it object a during the first execution of the garbage collector.
This object is marked as readable, and so are objects reachable from object a.
After some time, the garbage collector gets to work again and again.
It marks objects and its friends as switchable.
Then again, sometimes buses and garbage collector gets triggered again.
And one more time it marks objects as reachable.
If I were the garbage collector, I would probably get a bit frustrated.
This object is obviously a long grift, and I don't want to check if it's still needed every time I
get to work.
I have other things to do.
Well, that's exactly the optimization.
The garbage collector creators come up with to not make it check each object every time.
If some object survived a couple of cycles of the garbage collectors work, it's most likely a long-lived,
and we should check it only every once in a while.
This optimization introduced the concept of generations of objects.
Once an object is first created, it is assigned to Generation Zero if it survives its first collection.
It advances to generation one if it survives the second.
Its advances to generation to the garbage collector checks objects from generation zero most frequently
less frequently, its tookes objects from generation one and the least frequently from generation two.
It makes sense if the object survived two collections.
It's most likely long lived and there is a big chance it will survive collections number 10, 20 or
100.
We don't need to check that often.
For example, think of some larger objects.
Often there is one object created at the start of the program execution, and it is passed around to
other crafts that needs it.
Its lifecycle is basically as long as the time the application runs as the opposite.
We often create objects that lasts only for a very short time, like aligning with objects created to
carry some data between link quarries.
In this case, it is reasonable that the garbage collector will quickly remove them so they don't occupy
memory for too long.
In a world tuned application, most objects die in generation zero.
Please note that during a collection of a generation, all previous generations are also corrected,
so one generation zero is collected.
No other one is, but one generation two is collected.
Generation zero and one are two.
And that's why collecting objects from generation two is sometimes called a full garbage collection.
One more thing that needs to be mentioned is the large objects heap when a very large object so larger
than 85 thousands of birds is initially created.
It is stored in a special area of memory called the large objects heap, and it is assigned to generation
two right away.
It gets this special treatment because it really happens that very large objects are short lived.
Also, the objects in the large objects heap have one more special feature.
They are pinned.
It means they were meant to be moved in memory.
During that, the fragmentation step of the garbage collectors work what happens after removing and
reference objects from memory?
This is because the larger object is the more expensive is the operation of moving it, and the harder
it is to find the chunk of memory large enough to fit it.
It's better to pin such large objects and move smaller objects around them.
Let's summarize the garbage collector device objects into three generations zero, one and two.
Depending on their longevity.
Short lived objects belong to generation zero, and if they survived their first correction, they are
moved to generation one and after duct to generation to the garbage collector collects objects from
generation zero most often and from generation to earliest often.
This feature is introduced in order to improve garbage collector.
Performance objects that survived a couple of cycles of the garbage collectors work tend to be long
lived and they don't need to be looked upon so often.
This way, the garbage collector has less work to do, so it can do it faster.
During the interview, you might be asked What is the large objects?
Heap is the special area of the heap reserved for objects larger than 85000 bites.
Such objects logically belong to generation two from the very beginning of their existence and are pinched.
So what does it mean that the object is pinned?
It means it will not be moved during the memory, the fragmentation that the garbage collector is executing.
It is an optimization.
Ask what objects are expensive to move, and it's hard to find a chunk of memory large enough for them.
All right, that's it for this lecture.
Thanks for watching, and I'll see you in the next one.