Now when we know where the objects and the garbage resides, that is on the heap area, we can start a discussion 1
about collector types and the actual GC algorithms. 2
But before that, let's briefly talk about garbage types and see what actually a garbage means from specifically 3
the collectors perspective. 4
Because as we will see, there is also a special type of garbage which collectors simply can't identify. 5
So when we talked about unused objects on the heap, we differentiate Semantic and Syntactic garbage. 6
Let's see the difference. 7
The semantic garbage, despite being called the "garbage", represents actually the alive objects. Alive but 8
not used. 9
That is, it's a bug in the program logic, where a certain chunks of memory is allocated, but which is 10
never used. As an example 11
let's take a look at the issue of non-invalidated cache which is not used but which stays in memory. 12
So, we have against some object graph, and let's say at some point we decide that calculation of the object 13
B takes a lot of time. 14
So we go ahead and introduce a cache for it. 15
And later on we introduce another version of this cache. 16
However by some reason we forget to invalidate the previous one. At this point the cache C1 becomes a 17
semantic garbage. 18
And the problem here is that a garbage collector is simply not able to identify it as a garbage since 19
technically it's completely alive object. 20
So again, to repeat, semantic garbage relates to the bugs in the program logic. And the caches are not 21
the only example. 22
Let's take a look at this simple Stack implementation. 23
So, as we can see, we have the storage -- the elements array, and the size of the stack, which also plays the 24
role of the top of the stack. 25
The two standard API methods of the stack, which are the push and the pop methods are also trivial at 26
implementation. The push 27
actually adds a new element on top of the stack, and the pop method basically pops the last element 28
which is on top of the stack, decreasing the stack pointer, and returning this element back to the caller. 29
So by the end of the pop operation, we should have this elements removed from the stack. And this implementation 30
has actually a subtle bug. 31
In fact, the pop method doesn't actually remove the element. 32
It just decreases the stack pointer. And as long as we still have a reference to the popped object from 33
the outside, everything is okay. 34
However, let's say, at some point we have lost this reference. And at this point the object B becomes a 35
semantic garbage. 36
The problem here again is that we haven't actually removed the pointer to this object from our storage 37
array. And since the object still has a reference to it, it stays in memory. 38
And at this point it can even be considered a memory leak which cannot be identified by the garbage 39
collector, since again from the garbage collectors perspective 40
it is a reachable, that is, alive object. But that's not the end of the story. 41
Let's say, later we push a new element onto the stack, and at this moment the place, which was previously 42
occupied by the old object B, is now claimed by the new object C. 43
Now the old object is becoming a syntactic garbage, that is, a real unreachable object, and which a 44
garbage collector can already work with. So, casual stated, syntactic garbage is data that cannot be reached 45
while semantic garbage is data that will not be reached. And to when we consider different garbage 46
collection algorithms, we always and only work with the syntactic garbage, and use concept of reachability 47
of the objects. And the semantic garbage is in general an understandable problem, since we cannot precisely 48
predict, whether some alive data will or will not be used further in the program. 49
So again, garbage collectors do not work with this type of garbage. 50
Okay, now when we know about types of the garbage and which specific type we're going to work with, let's finally 51
talk about the Tracing and the Direct collectors, and discuss them in detail in the next video.