Прежде чем отправиться в путь, пожалуйста, найдите время, чтобы прочитать это,



Сборщик мусора работает с управляемой кучей, ничем иным, как блоком памяти, он проверяет мертвые объекты и объекты, которые больше не используются, затем сжимает пространство живого объекта и пытается освободить больше памяти.

По сути, куча управляется разными «поколениями», она хранит и обрабатывает долгоживущие и недолговечные объекты, см. следующие поколения кучи:

0 Generation (Zero): This generation holds short-lived objects, e.g., Temporary objects. GC initiates garbage collection process frequently in this generation.
1 Generation (One): This generation is the buffer between short-lived and long-lived objects.
2 Generation (Two): This generation holds long-lived objects like a static and global variable, that needs to be persisted for a certain amount of time. Objects which are not collected in generation Zero, are then moved to generation 1, such objects are known as survivors, similarly objects which are not collected in generation One, are then moved to generation 2 and from there onward objects remain in the same generation.
Reference - Stack Overflow

Решение GC о живых/мертвых объектах

GC проверяет следующую информацию, чтобы проверить, активен ли объект:

It collects all handles of an object that are allocated by user code or by CLR
Keeps track of static objects, as they are referenced to some other objects
Use stack provided by stack walker and JIT.

Когда срабатывает GC?

Для срабатывания сборщика мусора нет конкретных временных рамок, сборщик мусора автоматически начинает работу при следующих условиях:

When virtual memory is running out of space.
When allocated memory is suppressed acceptable threshold (when GC found if the survival rate (living objects) is high, then it increases the threshold allocation).
When we call GC.Collect() method explicitly, as GC runs continuously, we actually do not need to call this method.