Mark-and-sweep is a two-phase, tracing garbage collection algorithm that automatically reclaims memory allocated to objects no longer reachable by a program. In the mark phase, the collector traverses the object graph starting from root references (like global variables and stack frames) and marks every reachable object as 'in-use'. The subsequent sweep phase iterates through all allocated memory, deallocating any object not marked during the first phase, returning its memory to the free pool. This process prevents memory leaks by identifying unreachable, 'garbage' objects.
