Garbage collection

Meaning – The term garbage collection, refers to an automated memory management technique, that helps users identify unused areas of the main storage. In this, the garbage collector attempts to reclaim areas of memory that are occupied by objects no longer required by the particular program. Garbage collection relieves the programmer from performing manual memory management where the programmer specifies what objects to deallocate and return to the memory system and when to do so.

Resources other than memory, such as network sockets, database handles, user interaction windows, file and device descriptors, are not typically handled by garbage collection.

Garbage collection may also be done at compile-time when a program’s source code is compiled into an executable program. In this method, the compiler determines which resources in memory will never be accessed after a certain time. It can then add instructions to automatically deallocate those resources from memory.

Example of usage“A common method of garbage collection is reference counting. In this, a count is taken of how many instances of an object are stored in memory. If an object has zero references, it is considered unnecessary and can be deleted to free up the space in memory.”