Abstract
Dynamic memory allocation is traditionally managed by static heuristics such as first-fit, best-fit, and worst-fit. While effective in general cases, these heuristics are vulnerable to adversarial request patterns that lead to excessive fragmentation. This paper explores the application of Reinforcement Learning (RL) to this domain, framing memory management as a sequential decision-making problem. We evaluate RL agents both as low-level address selectors and high-level meta-allocators. Our results demonstrate that a Deep Q-Network (DQN) meta-allocator can learn to navigate complex request sequences, significantly outperforming standard heuristics in simulated adversarial environments.
Introduction
Memory management is a fundamental component of modern computing systems. Functions like malloc and free rely on allocation policies to manage a heap of available memory. Common heuristics include:
- First-fit: Allocates the first available block of sufficient size.
- Best-fit: Selects the smallest available block that fits the request to minimize immediate waste.
- Worst-fit: Selects the largest available block to maximize the size of the remaining fragment.
Despite their ubiquity, these static rules are susceptible to fragmentation. For any fixed heuristic, a specific sequence of requests can be constructed that causes allocation failure even when total free memory is ample. This paper investigates whether learning-based approaches can anticipate request patterns and manage memory more adaptively.
Methodology
We model memory allocation as a Markov Decision Process (MDP). The state is represented by a bitmap vector of the memory space, where 1 indicates an allocated block and 0 indicates a free block, concatenated with the size of the current request.
Two distinct action spaces were evaluated:
- Low-Level Control: The agent directly outputs the memory address for the allocation. This requires the agent to learn the spatial structure of memory from scratch.
- Meta-Allocation: The agent acts as a high-level controller, selecting which heuristic (first-fit, best-fit, or worst-fit) to apply for each incoming request.
We employed Deep Q-Networks (DQN) with Multi-Layer Perceptrons (MLP), Proximal Policy Optimization (PPO), and Linear Q-learning as a baseline. The reward function provides a small positive reward (+0.1) for successful allocations and a significant penalty (-10) for failures or invalid actions.
Experimental Results
The agents were tested against adversarial request generators designed to exploit specific heuristic weaknesses:
- Bf-good: Sequences where best-fit succeeds but others fail.
- Wf-good: Sequences where worst-fit succeeds but others fail.
Performance Comparison
The DQN meta-allocator demonstrated superior adaptability. In adversarial environments, it consistently matched or exceeded the performance of the best-performing heuristic for a given dataset. In the "Mixed-Adversarial" environment, the DQN + MLP policy achieved an average return of ~21, while traditional heuristics plateaued at 10 or less, indicating the agent serviced over 100 more requests before failure.
As shown in Figure 4, the DQN + MLP policy (a, c) demonstrates significant robustness. In the Wf-good scenario (a), the DQN policy even outperforms the worst-fit heuristic itself. Conversely, the Linear Q-learning baseline (b, d) struggles to generalize, particularly in the Bf-good environment, highlighting the non-linear nature of optimal memory allocation strategies and the necessity of deep learning models for this task.
Discussion and Future Work
Our findings suggest that RL can effectively mitigate fragmentation by learning to switch between heuristics based on the current memory state. However, several challenges remain:
- Scalability: Current experiments focused on small memory pages (up to 256 units). Scaling to gigabyte-scale heaps will require more efficient state representations, such as hierarchical or graph-based models.
- Latency: In production systems, allocation must be extremely fast. The computational overhead of a neural network inference per allocation may currently be prohibitive for kernel-level implementation.
- Temporal Dependencies: Interestingly, providing a simple history of request sizes did not significantly improve performance. Future work should explore Recurrent Neural Networks (RNNs) or Transformers to better capture long-term temporal patterns in memory requests.
Conclusion
This work demonstrates the potential of Reinforcement Learning to improve dynamic memory allocation. By treating allocation as a sequential decision process, RL agents can learn robust policies that outperform static heuristics in the face of adversarial workloads.
Summary updated