Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What is garbage collection?
Types of Garbage Collection
Reference Counting
Requirements of Garbage Collection
Type Safety
Characteristics of Garbage Collection
Classical Algorithms:
More Creative Algorithms
Frequently Asked Questions
What is garbage collection?
What is the main objective of garbage collection?
How many types of garbage collection are there?
Last Updated: Jun 20, 2024

Introduction to Garbage Collection

0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @

Garbage collection (GC) automatically manages memory by identifying and reclaiming unused memory blocks, preventing memory leaks. Garbage collection dates back to 1958 when Lisp was first implemented. Other garbage collection languages include Java, Perl, ML, Modula-3, Prolog, and Smalltalk.

Garbage collection refers to the process of automatically reclaiming memory occupied by objects that are no longer in use, typically performed by the runtime environment of a programming language.

Compiler Design

Programming languages such as Java and C# have a memory recovery feature called garbage collection (GC). Garbage collection is the process of automatically recovering computer memory storage. A garbage collector consists of a compiler and a runtime system. Your design should include the interface between the compiled code and the runtime system. A mark-and-sweep collector or a copying collector are viable options, but even a conservative collector is acceptable. Incremental collectors are far more challenging to implement and should only be tried if a basic collector is already in place.

Also see, Storage Organization

What is garbage collection?

Garbage collection is the process of restoring chunks of storage space that a program can no longer access. We assume that objects have a type that the garbage collector can determine at runtime. Based on the type information, we can determine the size of the object and which components of the object have references (pointers) to other objects. We also assume that all references to objects correspond to the address of the object's beginning rather than pointers to specific locations within the object. Therefore, all objects' references have the same value and are easily identifiable.

We'll refer to a user application as the mutator, which modifies the heap's collection of objects. The mutator generates objects by obtaining space from the memory manager, and it can add and remove references to existing objects. When the mutator can't "reach" an object, it becomes garbage.

The garbage collector locates these inaccessible objects and returns them to the memory manager, which keeps track of the available space.

Types of Garbage Collection


The mark-and-sweep algorithm involves two phases: marking and sweeping. During the marking phase, the algorithm traverses the object graph starting from the root, marking all reachable objects. In the sweeping phase, it scans the heap for unmarked objects and reclaims their memory, making it available for future allocations.


The copying garbage collection algorithm divides the heap into two halves. It allocates objects in one half, and when it runs out of space, it copies live objects to the other half, leaving behind the unused space, which gets reclaimed in bulk. This process reduces fragmentation and is efficient for memory allocation but can be less space-efficient due to the division of the heap.

Reference Counting

Reference counting maintains a count of references to each object. When an object's reference count drops to zero, it is considered unreachable and can be immediately reclaimed. While simple and providing immediate reclamation, reference counting has drawbacks, such as handling cyclic references, where objects reference each other, preventing their reference counts from reaching zero.


Generational garbage collection divides objects into generations based on their lifespan. It assumes that most objects die young, so it focuses on collecting young objects more frequently. The heap is divided into young and old generations, with minor collections occurring frequently in the young generation and less frequently in the old generation. This approach optimizes performance by reducing the overhead of frequent garbage collections.


Incremental garbage collection breaks the work of garbage collection into smaller chunks, interleaving it with the program's execution. This reduces pause times and improves the application's responsiveness. The collector incrementally marks and sweeps objects, allowing the program to run concurrently with the garbage collection process.


Concurrent garbage collection runs alongside the application, minimizing pause times and improving responsiveness. It allows the program to continue executing while the garbage collector identifies and reclaims unreachable objects. This approach is complex to implement but provides significant performance benefits, particularly for interactive applications.

Want to learn about Parallel Operating System click here.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Requirements of Garbage Collection

Type Safety

Not all languages are suitable for automatic garbage collection. A garbage collector must determine whether every given data element or component of a data element is or may be used as a pointer to a piece of allocated memory space to function correctly. A type-safe language is one in which any data component can be determined. 

Type-safe languages, such as ML, allow us to decide types at compile time. These are called statically typed languages. Other typesafe languages, such as Java, have types that can be decided during runtime rather than compile time. These are called dynamically typed languages. If a language is neither statically nor dynamically type-safe, it is unsafe.

  1. Garbage collection is important for avoiding fragmentation and making the most available memory.
  2. Garbage collectors are known for causing programs — the mutators — to halt for an unusually long time when garbage collection occurs without warning. As a result, in addition to reducing total execution time, it is preferable to reduce the maximum pause duration. 
  3. Real-time applications are specific scenarios in which certain computations must be finished within a certain amount of time. We must either disable garbage collection while executing real-time tasks or limit the pause time. As a result, garbage collection is only employed in real-time systems.
  4. We can't evaluate a garbage collector's speed based on its running time. The garbage collector is in charge of data placement, impacting the mutator program's data locality. It can increase the temporal locality by freeing up space and reusing it; it can improve the geographical locality by moving data used together in the same cache or pages.

See, Static Allocation

Characteristics of Garbage Collection

  1. Many programmers argue that automatic garbage collection is inefficient compared to explicit storage reclamation. However, several studies have shown that well-implemented garbage collectors outperform systems with explicit deallocation.
  2. Automatic deallocation relieves a programmer of memory management concerns, improving system writability and reducing development time and costs.
  3. Garbage collection is required for functional languages that cannot use a stack-based environment due to unpredictable execution patterns.
  4. Garbage collection is generally so expensive that, even though it was established decades ago and effectively avoids memory leaks, many prominent programming languages have yet to implement it.
  5. Garbage collection can take a long time. It does not significantly increase an application's total execution time. Since the garbage collector must deal with a large amount of data, using the memory subsystem significantly impacts its speed.


Many algorithms can be used to implement automatic garbage collection in computer software. Some of them are given below.

Classical Algorithms:

  1. Reference Counting
  2. Mark-Sweep
  3. Mark-Compact
  4. Copying Collection
  5. Non-Copying implicit collection

More Creative Algorithms

  1. Incremental Tracing Collection
  2. Generational Collection

We will read them in detail in the later articles.

Frequently Asked Questions

What is garbage collection?

Garbage collection is the process of recovering pooled computer storage that has been used by a program but is no longer required by that application. This frees up storage for other applications. 

What is the main objective of garbage collection?

The primary goal of a Garbage Collector is to clear heap memory by eliminating unreachable items.

How many types of garbage collection are there?

There are several types of garbage collection techniques, including mark-and-sweep, copying, reference counting, generational, incremental, and concurrent, each with its own advantages and trade-offs in terms of efficiency and performance.


In this article, we learned about Garbage collection. We learned that garbage collection is a process of recovering pooled computer storage that has been used by a program but is no longer required by that application. This frees up storage for other applications. 

Garbage collection languages include Lisp, Java, Perl, ML, Modula-3, Prolog, and Smalltalk.

We also learned about the key points of the design goals of a garbage collector. Then we moved on to the characteristics of garbage collection algorithms.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Next article
Trace-Based Garbage Collection
Live masterclass