Garbage Collection
Memory management is one of the most important aspects of modern programming. Almost any language that you invent should provide automatic heap memory management via garbage collection. Garbage collection refers to any mechanism by which heap memory is automatically freed and made available for reuse when it is no longer needed for a given purpose. The heap, as you may already know, is the region in memory from which objects are allocated by some explicit means such as the reserved word new
(in Java). In lower-level languages, such objects live until the program explicitly frees them, but in many modern languages, heap objects are retained in memory as long as they are needed. After a heap object is of no further use in the program, its memory is made available to the program for other purposes by a garbage collection algorithm that runs behind the scenes in the programming language runtime system.
This chapter presents a couple of methods with which you can...