Mark-and-Sweep
Mark-and-Sweep is a garbage collection algorithm used in programming languages to manage memory. It works in two phases: the "mark" phase identifies all the objects that are still in use, while the "sweep" phase removes objects that are no longer reachable, freeing up memory.
During the mark phase, the algorithm traverses the object graph, starting from root objects, and marks all reachable objects. In the sweep phase, it scans through the memory, collecting unmarked objects and reclaiming their space. This process helps prevent memory leaks and optimizes memory usage in applications.