arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2403.06695cs.PL
收藏

堆 vs. 栈:C 和 C++ 开源软件中的内存分配分析

Heap vs. Stack: Analyzing Memory Allocations in C and C++ Open Source Software

Roman Korostinskiy, Eugene Darashkevich, Roman Rusyaev, Yegor Bugayenko

AI总结:

该研究针对C和C++开源项目的内存分配展开,通过测试23个项目的797个用例,发现堆分配占比差异大且C++用堆频率低于C,打破了程序员会主动规避堆分配的假设。

摘要:

在 C++ 中,对象可以分配在静态内存、栈上或堆上——其中堆分配的性能成本远高于前两种方式。我们假设程序员,尤其是参与广泛使用的开源项目的程序员,会意识到这些性能成本,从而避免堆分配。为验证该假设,我们在 GitHub 上的 13 个 C 语言和 10 个 C++ 开源项目中编译并执行了 797 个自动化测试,使用 Valgrind 测量堆分配,借助 DynamoRIO 插桩测量栈分配。我们的发现显示堆分配占比差异很大,范围从 0 到 99%,平均值为 9.26%。我们还发现 C++ 程序使用堆的频率低于 C 程序。这与我们最初的直觉相反,表明堆分配在 C 和 C++ 程序中都被积极使用。确定这些分配中对象的普遍程度仍是未来研究的课题。

英文摘要:

In C++, objects can be allocated in static memory, on the stack, or on the heap -- the latter being significantly more performance-costly than the former options. We hypothesized that programmers, particularly those involved in widely-used open-source projects, would be conscious of these performance costs and consequently avoid heap allocations. To test this hypothesis, we compiled and executed 797 automated tests across 13 C and 10 C++ open GitHub projects, measuring their heap allocations with Valgrind and stack allocations using DynamoRIO instrumentation. Our findings showed a wide variation in heap allocations, ranging from 0 to 99\% with an average of 9.26\%. We also found that C++ programs use heap less frequently than C programs. Contrary to our initial intuition, this suggests that heap allocations are actively employed in both C and C++ programs. Determining the prevalence of objects in these allocations remains a topic for future research.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑