Abstract
Garbage collection (GC) is a fundamental component of modern programming languages, yet most implementations rely on static, application-blind heuristics. This paper presents the first end-to-end garbage collection policy trained entirely via reinforcement learning (RL) during program execution. By replacing hard-coded triggers with a tabular Q-learning agent, the system autonomously discovers optimized collection policies tailored to specific application behaviors and user-defined performance metrics.
Introduction
Garbage collection automatically manages memory allocation and deallocation, freeing developers from manual memory management. However, traditional "one-size-fits-all" GC policies often lead to sub-optimal performance, such as high tail latency in web servers or frame drops in interactive applications. Researchers from MIT CSAIL and Intel Labs have proposed a "learned" garbage collector that adapts to the unique rhythm of an application to optimize metrics like throughput and latency.
Problem Statement
Generational garbage collectors, such as those in CPython or Java, typically use fixed thresholds to trigger memory scans. These heuristics do not account for application-specific context. For instance, a web server might benefit from delaying GC until after a request is served, while a background task might prefer frequent, small collections. Manually tuning these parameters is complex and rarely done by developers. The goal of this research is to create a collector that learns "opportune moments" for memory reclamation based on real-time performance feedback.
Methodology
The researchers implemented a prototype within CPython 3.7.5, modeling GC as a Markov Decision Process (MDP). The agent's state is defined by the current allocation site (program counter) and memory usage. Available actions include doing nothing or collecting a specific memory generation (G1, G2, or G3).
To minimize overhead, the system utilizes tabular Q-learning rather than deep neural networks. A Q-table allows for nanosecond-level lookups, which is essential for a process that occurs hundreds of times per second. Three key optimizations were introduced to make the RL agent practical:
- Priors (P): Biasing the agent toward "no action" to prevent excessive collection.
- Reward Shaping (S): Penalizing long GC cycles to encourage efficient collection points.
- Table Initialization (I): Pre-seeding the table with basic knowledge (e.g., avoiding out-of-memory errors) to accelerate learning.
Evaluation and Benchmarks
The learned collector was evaluated against four benchmarks representing different memory patterns:
- LRU Cache: A synthetic program with cyclic references and large objects.
- Webserver: A synthetic HTTP server handling small requests.
- Tx: A complex transaction graph search, representing a challenging case for GC.
- ProjMang: A real-world scenario using the Taiga.io project management toolkit.
Results and Insights
The RL agent demonstrated a remarkable ability to discover application-specific optimizations. In the Webserver benchmark, the agent learned to trigger GC immediately after queuing a response for delivery, effectively overlapping the "stop-the-world" pause with network I/O wait times.
Key performance gains included:
- Webserver: A 268.2% improvement in requests per second.
- LRU Cache: A 25.48% increase in throughput.
- ProjMang: A 5.42% improvement on a real-world application.
In the Tx benchmark, the agent achieved parity with the default collector (-0.01% difference), as the memory structure left little room for timing-based optimization.
Conclusion and Future Work
This research proves that classical tabular RL can significantly outperform static heuristics in systems tasks where low overhead is critical. While the current implementation introduces some memory overhead (4-18MB for the Q-table) and requires a "warm-up" period for each program, it paves the way for "self-driving" runtimes. Future work may explore transfer learning to provide agents with pre-trained general policies and methods to scale the state space for massive applications.
Summary updated