As we have discussed in the previous video about allocation types dynamic memory that is the memory 1
allocated on the heap might be hard to manage. 2
There are two major problems related to the manual memory management and both are actually related to 3
the human factor. 4
So in this video we're going to take a closer look at the issues of the manual allocation and deallocation. 5
Let's say we have the following object graph on the heap. So we have some root pointing object A which 6
points object B which in turn points the object C. It might be a linked list or a similar data structure. 7
During our discussions in the course we'll be actively using such object graphs and to also operate 8
with the concept of the garbage collector roots. A root is a known reachable reference which is usually 9
hold by a register or a variable on the stack. 10
And in this case only the object A is a root, and B and C are not roots. That is, they are not referred 11
directly by a register or a variable and instead are appointed from some other objects on the heap. 12
And then let's say at some point we decide to remove the object B by calling the free function on the 13
A->B pointer. 14
But now what's happened to the object C? Being now unreachable from any root, it's becoming a Memory Leak and 15
that is the first issue of the manual memory management. 16
What was the actual problem here? 17
The human factor. We are, being non-attentive or yet non-experienced engineers, completely forgot that 18
before we are allowed to removing the object A->B, we have to first follow the child pointer of B and remove 19
the B->C object. And only after that we can safely remove the A->B object. 20
Okay let's say at some point we became such experienced engineers and know that before we can remove 21
the object A->B, we have to first remove the B->C object. But now we've got a more complicated case: a second 22
root, the Root 2 is also pointing let's say the object B. From the perspective of the Root 1 we did 23
everything correct -- removed the object chain in the needed order. But now what we have got here from the 24
Root 2 reference is known as a Dangling Pointer, and that is the second issue of the manual memory management. 25
Okay let's do a quick recap of the issue and see again the key points. 26
So this is our problem statement. 27
The memory leaks and the dangling pointers. 28
Notice that despite being very related these two issues are actually completely different. In the first 29
case we have more memory on the heap than we need. 30
That is we've got leaking memory. In the second case we have less memory on the heap than we need. 31
That is we've got an invalid or dangling pointer. 32
Again the root cause of the problem is the human factor. As we have seen it might be hard to maintain 33
complex data structures and free objects correctly. 34
So we're going to eliminate manual management and replace it with automated solution known as the garbage 35
collection. 36
Now when we have identified and understood the problem we need to fix 37
let's see at how we're actually going to fix it. 38
We start our discussion with the extra data structure known as the Object header, which is used to maintain 39
the extra state needed for the collectors or allocators purposes and which we'll be discussing in the next 40
video.