How garbage collection works
Computer programs allocate memory for data objects: they stake their claim to certain areas in memory, telling the rest of the system "this memory is in use, and no other programs can use it." When the program no longer needs this area of memory, it must be "released" before it can be used by other data objects, or system processes. In some programming languages, this must be done manually (the programmer must explicitly allocate and free up memory as part of the program itself).
Garbage collection automates this process by using an agent process (the "garbage collector") to search for allocated memory that could not possibly be used by the program in the future. It identifies this "stale" memory and frees it, making it available to be used for something else.
Some programming languages require garbage collection as part of their specification (for instance Java, C#, and Go). Other languages were designed for manual memory management, but have garbage-collected implementations (like C and C++). Still, other languages such as Ada allow garbage collection and manual memory management to co-exist within the same application.
Benefits of garbage collection
Garbage collection has the advantage of eliminating human error. Programs that manage memory manually may contain errors which access it incorrectly, causing the program to crash, or exposing the system to security vulnerabilities. However, garbage collection requires additional system resources, and the collection itself may occur at unpredictable times, making the speed and efficiency of programs less reliable.