How does garbage collection works

Garbage collection uses a data structure called Bloom filters, some explanations are provided in this thread: How bloom filters work