PTE · Reading · medium
Read the text and answer the multiple-choice question by selecting the correct response. Only one response is correct.
In automated memory management, modern programming runtimes rely on garbage collection to reclaim memory occupied by objects that an application no longer requires. Without this mechanism, programs would either suffer from memory leaks—gradually consuming all available system memory—or crash due to dangling references caused by premature manual deallocation. One of the most prevalent approaches is tracing garbage collection, commonly implemented through the mark-and-sweep algorithm. The process begins from a designated set of root references, such as global variables and active pointers on the execution call stack. During the mark phase, the collector traverses the application's object graph, visiting every reachable object and setting an internal flag or bit. Any memory chunk that cannot be reached through this chain of references is deemed inaccessible and thus obsolete. In the subsequent sweep phase, the collector scans the heap, identifying unflagged memory blocks and returning them to the pool of free memory for future allocation. Although this automated routine liberates developers from manual resource bookkeeping, it introduces brief runtime pauses while the object graph is traversed, necessitating ongoing refinements in concurrent and generational collection designs.
Which statement best describes the function of the 'mark' phase in a mark-and-sweep garbage collector?
- It immediately deletes unreferenced objects to free up heap capacity.
- It flags objects that can be accessed starting from known root references.
- It pauses user interactions to allocate additional memory for stack pointers.
- It compiles dynamic references into static machine code for faster traversal.