Predicting Lifetimes in Dynamically Allocated Memory

David A. Cohn, Satinder SinghNeurIPS 1996

Introduction

In most software, memory management is a "one-size-fits-all" affair. When a program requests memory (e.g., via malloc), the system provides a block without knowing if it will be needed for two milliseconds or two hours. This lack of foresight leads to significant CPU overhead from managing a general-purpose heap and causes memory fragmentation when short-lived objects are mixed with long-lived ones.

While previous attempts to solve this (notably by Barrett and Zorn in 1993) used simple heuristics based on allocation sites, these methods were often too rigid to generalize across different program behaviors. This paper demonstrates that machine learning—specifically decision trees—can accurately predict the lifespan of dynamically allocated memory objects by analyzing the program's execution state. By identifying "short-lived" vs. "permanent" objects at the moment of allocation, systems can use specialized, high-performance allocators to reduce fragmentation and improve runtime efficiency.

Methodology

The authors treat memory lifetime prediction as a classification problem, utilizing the OC1 decision tree software. Unlike previous methods that relied on a fixed-depth call graph, decision trees can evaluate a wide array of features to automatically determine which ones matter most for prediction.

For every memory request, the system captures the request size and the top 20 words on the program stack. The stack words provide a snapshot of the program's current state, including function arguments and return addresses. Objects are then classified into three categories:

  • Short-lived: Freed before 32KB of subsequent allocations occurred.
  • Permanent: Never freed until the program terminated.
  • Long-lived: Everything else.

The authors built a hybrid system: if an allocation site consistently produced the same type of object, it used a specialized allocator. If the site produced mixed results, a site-specific decision tree made a real-time prediction. The model was trained and tested on a suite of public-domain, allocation-intensive benchmark programs, including Ghostscript, Espresso, Cfrac, Gawk, Perl, and Gcc. Different input files were used for training and testing to ensure the model generalized well rather than just memorizing execution paths.

Key Insights

  • The Stack as a Feature Vector: Using the raw program stack as input for a decision tree is a highly novel approach. While the stack contains noise, decision trees excel at feature selection—ignoring irrelevant data and splitting only on the specific stack words that correlate with object lifetime.
  • Beyond the Call Site: Previous work identified allocation sites by the sequence of function calls. This research shows that other state information, such as function parameters or local variables on the stack, can be even more predictive.
  • The Cost of Misprediction: Not all errors are equal. Predicting a permanent object as short-lived is dangerous because it can "clog" the fast allocator's memory space, eventually slowing the system down more than if no prediction had been made.

Results and Performance

The decision tree approach significantly outperformed the previous state-of-the-art heuristics by Barrett & Zorn.

Comparison of false positive and false negative rates between Barrett & Zorn and the OC1 decision tree model.

As shown above, the OC1 model drastically reduced error rates. For example, in the espresso benchmark, the previous method had a 72% false negative rate for short-lived objects; the decision tree slashed this to just 6.58%. In practical tests, the specialized "quick" allocators were roughly four times faster than standard malloc.

Benchmark test errors and runtime comparisons between normal and predictive execution.

When evaluating real-world gains, the system showed clear improvements in total execution time for applications like ghostscript and gcc. However, the results also highlighted the critical importance of accuracy. For cfrac, lower prediction accuracy led to "memory pollution," which degraded performance on longer runs (increasing runtime from 225.31 to 245.64 seconds), proving that predictive accuracy is paramount in systems applications.

Limitations and Future Work

  • Input Sensitivity: Programs like Perl and Gawk proved difficult to predict because their memory behavior changes drastically depending on the script and data they process. The model struggled to generalize across vastly different scripts.
  • The "Relevant Feature" Mystery: While the decision trees worked effectively, the authors have not yet analyzed why certain stack words were predictive. Understanding the semantics of these relevant features remains a task for future research.
  • Hardware Specifics: The study was conducted on a Sparcstation. Compiler optimizations (like omitting frame pointers) can change what information is available on the stack, potentially affecting the model's portability to other architectures.

Summary updated

BibTeX
@inproceedings{cohn1996predicting,
  author = {David A. Cohn and Satinder Singh},
  title = {{Predicting Lifetimes in Dynamically Allocated Memory}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {1996},
}
Figure