Inferensys

Glossary

Garbage Collection (GC)

Garbage collection (GC) is an automatic memory management process that identifies and reclaims memory occupied by objects no longer in use by a program.
Knowledge manager reviewing enterprise knowledge management system on laptop, document library visible, casual office.
MEMORY UPDATE AND EVICTION

What is Garbage Collection (GC)?

Garbage collection (GC) is an automatic memory management process that identifies and reclaims memory occupied by objects that are no longer in use by the program.

Garbage collection (GC) is an automatic memory management process that identifies and reclaims memory occupied by objects that are no longer in use by the program, preventing memory leaks. In agentic systems, GC is critical for managing the ephemeral state within a working set, such as intermediate reasoning steps or obsolete conversation context, ensuring efficient use of limited context windows. It operates by tracing object reachability from root references, like active agent variables or session data.

Common GC algorithms include mark-and-sweep and generational collection, which optimize for different access patterns. For agentic memory, GC policies must balance latency against memory footprint, often integrating with cache eviction policies like LRU or TTL to manage vector store indexes or knowledge graph nodes. This prevents thrashing and maintains system responsiveness by autonomously freeing resources for new computations and state updates.

MEMORY UPDATE AND EVICTION

Key Garbage Collection Algorithms

Garbage collection automates memory reclamation by identifying and freeing objects no longer in use. Different algorithms balance throughput, latency, and memory efficiency for various workloads.

01

Mark-and-Sweep

The foundational tracing algorithm that operates in two distinct phases. In the mark phase, the collector traverses the object graph starting from root references (global variables, stack frames) and marks every reachable object. In the sweep phase, it scans the entire heap, deallocating memory for any object not marked as reachable.

  • Advantages: Simple to implement, handles cyclic references.
  • Disadvantages: Requires pausing the application (stop-the-world), can cause memory fragmentation.
  • Example: Early implementations of Java Virtual Machine (JVM).
02

Generational Collection

A performance optimization based on the weak generational hypothesis, which observes that most objects die young. The heap is divided into generations (e.g., Young Generation, Old Generation). New objects are allocated in the young generation, which is collected frequently with a fast minor GC. Objects that survive multiple collections are promoted to the old generation, which is collected infrequently via a slower major GC.

  • Key Benefit: Reduces pause times by focusing effort where most garbage is created.
  • Common in: Modern JVMs (HotSpot), .NET CLR.
03

Reference Counting

A direct, non-tracing algorithm where each object maintains a count of references to it. The count is incremented when a new reference is created and decremented when a reference is destroyed. When an object's count reaches zero, its memory is immediately reclaimed.

  • Advantages: Immediate reclamation, predictable pauses.
  • Disadvantages: Cannot reclaim cyclic references (e.g., object A references B, and B references A), overhead on every pointer assignment.
  • Use Case: File handles, COM objects, Python's primary GC mechanism (used with a cycle detector).
04

Copying Collection (Cheney's Algorithm)

A tracing collector that divides the heap into two equal-sized spaces: from-space and to-space. Allocation happens only in from-space. When it fills, the collector copies all reachable objects from from-space to to-space, compacting them in the process. The roles of the spaces are then swapped.

  • Advantages: Eliminates fragmentation, collection time proportional to number of live objects.
  • Disadvantages: Uses only half the available heap for allocation at any time.
  • Typical Use: Often used for the young generation in a generational collector.
05

Concurrent Mark-Sweep (CMS)

Designed to minimize stop-the-world pauses by performing most garbage collection work concurrently with the application threads. It has phases:

  1. Initial Mark: Brief pause to mark roots.
  2. Concurrent Mark: Marks reachable objects while app runs.
  3. Remark: Brief pause to catch updates made during concurrent mark.
  4. Concurrent Sweep: Reclaims memory while app runs.
  • Trade-off: Reduces pause times but increases CPU overhead and can lead to floating garbage. Deprecated in favor of G1 and ZGC in modern JVMs.
06

Garbage-First (G1) Collector

A regionalized and predictable low-pause collector for multi-processor machines with large memories. It divides the heap into equal-sized regions. G1 tracks the amount of live data in each region and prioritizes collection in the regions with the most garbage first (hence the name). It aims to meet user-defined pause time targets.

  • Key Mechanism: Mixed collections that can include both young and old generation regions.
  • Operation: Uses remembered sets to track references into a region, limiting scan scope.
  • Default in: Java 9+ for server-style deployments.
GARBAGE COLLECTION

Frequently Asked Questions

Garbage collection (GC) is an automatic memory management process that identifies and reclaims memory occupied by objects that are no longer in use by the program. This FAQ addresses its core mechanisms, trade-offs, and role in modern systems, particularly within agentic and AI architectures.

Garbage collection (GC) is an automatic memory management process that identifies and reclaims memory occupied by objects that are no longer in use by the program, preventing memory leaks. It works by periodically executing an algorithm that traverses the object graph starting from root references (like global variables and active stack frames). Objects reachable from these roots are considered 'live'; all others are 'garbage' and their memory is scheduled for reclamation. Common algorithms include mark-and-sweep, copying collection, and generational collection, each with different strategies for identifying garbage and compacting free memory.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.