Welcome back. 1
In the previous video we have identified two problems related to the manual memory management: the memory 2
leaks and the dangling pointers. 3
And we also agreed to solve them by automating the collection process. Usually any automation comes with 4
some price, trading for example storage for speed or speed for storage, or even speed and storage for robustness 5
of a program etc. And in case of the garbage collectors it is a special meta-information which we're 6
going to store with each object, and which is known as the Object header. 7
In general the object header 8
can be used by both, collector and allocator. 9
Let's consider this `malloc` allocation of 5 bytes. 10
In fact even if we pass exact amount of bytes to allocate in reality the actual number of allocated 11
bytes may differ. 12
So let's take a look at what's happened and under the hood of such allocation. 13
First we have our 5 bytes fairly allocated, here. Next 14
since the memory should be aligned by the size of the machine word, we get here 15
something known as the padding or the alignment. On a 32 bit machine where the word size is 4 bytes 16
this allocation already takes 2 words, that is 8 bytes, and that's why we get the 3 bytes alignment here. 17
And finally here we get our object header: as we said the meta-information which can store any extra 18
state needed for the garbage collectors or allocators purposes. 19
And this may add extra N bytes depending on your implementation. 20
So the total size of such allocation might be 16, 24 or even more bytes. 21
To reiterate the Object header may store any meta-information. For example, it might be the Mark-bit 22
used in the Mark-Sweep collector, which we'll be discussing in detail further in this class. 23
It might be the Reference counter used by the Reference counting collector. 24
It might be the flac whether this memory block is free or not -- used by the allocator, etc: depending 25
on the GC algorithm 26
and again on your implementation. But for now just memorize that such thing as the Object header exists 27
and which we'll be actually using further in description of the garbage collection algorithms. 28
Now we should also say that the placement of the Object header in memory may differ. Usually as in this 29
example it is co-located with the actual memory block. 30
In our case it's stored prior the data block, but in general it can be located anywhere. 31
Okay now when we know which extra data structure we need in order to automate the collection process. 32
Let's talk about the Memory layout, see where the heap area resides, and how we're actually going to 33
collect the garbage in it.