Back

Garbage Collection

Idealogic’s Glossary

Garbage Collection is memory management that is done automatically in a program, portion of the application that is no longer needed is reclaimed. This process is useful in avoiding memory congestion where a program retains memory that is no longer in use and there is consequential use of all the available memory and subsequent system failure.

How Garbage Collection Works 

In all the programming languages which have the feature of garbage collection, like Java, Python and C#, memory management is done automatically with the help of GC. In the course of a program, objects/variables are created and memory space is reserved for their storage. It also ensures that when the program is running, it does not have some of the objects or variables that may have been created and assigned memory space are no longer useful or may have gone out of scope, the garbage collector takes it upon itself to free the memory space for future use. 

The garbage collection process generally involves the following steps

Identification of Unreachable Objects

At first these objects that are no longer in use or required by the program are identified by the garbage collector. An object is said to be garbage if there are no pointers to it from any part of the program. This usually involves following the so called ”object graph” starting from a set of so called ”root references” – active variables and function calls – and marking all objects that can be reached. 

Reclamation of Memory

When such objects, which cannot be reached anymore are identified then the garbage collector releases a memory that is occupied by these objects. This way the memory that has been released by the objects is then used for allocation of new objects which are to be created by the program in the future. 

Memory Compaction

Some of the GC implementations may also include the compacting of memory in order to reduce fragmentation. Fragmentation is the condition in which free memory is partitioned into several regions in the memory space whereby it becomes difficult to allocate a large block of free memory. Compaction is the process of moving objects in the memory and arrange them in a way that free spaces are closed together. 

Types of Garbage Collection 

There are several different strategies for garbage collection, each with its advantages and trade-offs

Reference Counting

This is one of them, which relies on the so called reference counting, where each object keeps a counter that counts the number of references to it. When the reference count is equal to zero then that object is regarded as garbage and can be recycled. Nevertheless, there is one particular issue that may cause trouble – circular references, when two or more objects refer to each other and therefore the reference counter never drops to zero. 

Mark-and-Sweep

In this case the Garbage Collector just have to begin with the identification of all accessible objects from the root set and then search the memory for the objects which are not accessible. It is efficient and can cope with circular references but, it can cause the program to hang while the GC is running particularly in large applications. 

Generational Garbage Collection

This technique is founded on the fact that the majority of the objects in the typical program live for a short duration. It categorizes objects by their age (e. g. young, old) and compacts the memory more often in the young generation to which most of the objects belong and become unreachable soon. Using this approach can assist in increasing performance since attention is focused on the part of memory, which is probably contains garbage.

Incremental and Concurrent Garbage Collection

These techniques try to minimize the so called stop-the-world pauses which are forced by the garbage collection, by dividing the work into smaller parts so that garbage collection happens in smaller chunks (incremental) or by running the garbage collector in parallel to the program (concurrent). Doing this makes garbage collection occur in the background and hence makes the program to be freer responding.

Importance of Garbage Collection 

It is critical in the current world of programming because it assists in the elimination of memory leaks and maximizing on memory usage. When garbage collection is not automatic, programmers would have to allocate and de-allocate memory on their own which is very risky and can cause memory leak, wild pointers and program crashes. 

Other languages like the C and C++ do not support the automatic garbage collection of memory, and this makes the programmer handle memory allocation and deallocation manually, and this leads to more complicated and prone to error code. However, there is a flip side of garbage collection and that is, it is not free of charge. This can cause overhead which may result to inefficiency especially in real time systems or in applications that need to have an assured throughput. 

Conclusion 

Therefore, the Garbage Collection is a useful mechanism for the automatic memory management in programming that eliminate memory that is no longer in use which helps avoid memory leaks and improve application’s performance. Garbage collection helps in the easy and efficient working of programs by freeing up memory that is no longer required and can be done away with thereby making the work of the developer easier and less time consuming. Nevertheless, it leads to some drawbacks especially the performance issue that the developers should take into account during the application designing and development.