What is Garbage Collection?
Garbage Collection (GC) is a dynamic approach to automatic memory management and heap allocation that processes and identifies dead blocks of memory and reallocates memory for reuse. The main purpose of garbage collection is to reduce memory leaks.
GC implementation requires three primary approaches as follows:
Mark and Sweep - When the memory runs out, the GC searches all available memory and then reloads the available memory.
Reference Count - Assigned objects contain a reference count of the reference number. If the memory count is zero, the object is garbage and is then destroyed. The freed memory is returned to the memory heap.
Copy Collection - There are two storage partitions. When the first partition is full, the GC finds all accessible data structures and copies them to the second partition, compressing the memory after the GC process and freeing up continuous free memory.
Some programming languages and platforms with integrated GC (e.g. Java, Lisp, C # and. Net) manage memory leaks themselves and thus enable more efficient programming.
The dynamic approach of garbage collection to automatic heap allocation fixes common and costly errors that often lead to real program errors unnoticed.
Because they are difficult to identify and repair, mapping errors are costly. As a result, garbage collection is seen by many as an essential language feature that makes the programmer's job easier with less manual management of heap allocations. However, GC is not perfect and the following drawbacks should be considered:
GC consumes computing resources when freeing memory.
The GC process is unpredictable, resulting in scattered session delays.
If unused object references are not manually removed, GC will cause logical memory leaks.
GC does not always know when to process it in the virtual storage environment of modern desktop computers.
The GC process interacts poorly with cache and virtual storage systems, which leads to performance adjustment difficulties.