可动态迁移数据内存分配器的设计与实现
Design and Implementation of a Memory Allocator with Dynamically Migration Data
摘要: 动态内存分配是程序运行的核心需求,本文采用C++语言设计并实现了一种可动态迁移数据的内存分配器,按照由简到繁的思路给出三种实现方案。第一种是竞技场模式的贪心分配器,只分配不释放,仅在复用整个空间时重新标记为空闲;第二种是简单回收分配器,释放时将当前块标注为空闲并与前驱空闲块合并,当空间不足时进行碎片整理,并通过分配时回传的地址指针自动将旧地址改写为新地址,使数据迁移对使用者完全透明。第三种是稳定回收分配器,通过分配时依次试探小空闲块、大空闲块与波前空间,以优先复用最近释放空间,减少波前空间消耗和碎片整理次数。对动态数组的算例测试表明,三种分配器的执行速度均优于系统默认分配器,还具备简单的内存诊断功能,不仅可用于常规内存,还可用于非易失性存储介质,具有广泛的适用性。
Abstract: Dynamic memory allocation is a core requirement for program execution. In this paper, a memory allocator capable of dynamically migrating data is designed and implemented in C++, and three implementations are presented progressing from the simple to the complex. The first is a greedy allocator operating in arena mode, which only allocates and never frees, remarking space as idle only when the entire arena is reused. The second is a simple reclamation allocator: on release it marks the current block as idle and merges it with the preceding idle block, and when space is insufficient it performs defragmentation, automatically rewriting the old address to the new one through the address pointer passed back at allocation time, so that data relocation is completely transparent to the user. The third is a stable reclamation allocator, which probes the small idle block, the large idle block, and the wave-front space in turn during allocation, preferentially reuses the most recently freed space, and thereby reduces wave-front space consumption and the number of defragmentation operations. Tests on an example of dynamic arrays show that all three allocators execute faster than the system default allocator and that they additionally provide a simple memory diagnostic capability. They can be used not only for conventional memory but also for non-volatile storage media, and therefore have broad applicability.
参考文献
|
[1]
|
张琼声, 刘冬萍. 操作系统内核内存分配算法的分析与性能评价[J]. 计算机系统应用, 2007(1): 40-43.
|
|
[2]
|
王小银, 陈莉君. Linux内核中内存池的实现及应用[J]. 西安邮电学院学报, 2011, 16(4): 40-43.
|
|
[3]
|
何煦岚, 何晓岚. 基于多链表结构的嵌入式系统内存管理[J]. 计算机应用与软件, 2008, 25(4): 58-59+81.
|
|
[4]
|
孙棣华, 吴文峰, 郑林江, 等. 一种嵌入式实时系统动态内存管理器的设计[J]. 小型微型计算机系统, 2014, 35(5): 1106-1110.
|
|
[5]
|
娄云龙. 用户态内存管理关键技术研究[D]: [硕士学位论文]. 哈尔滨: 哈尔滨工程大学, 2014.
|
|
[6]
|
肖钦定, 余小喜. 基于C++自定义内存分配器的实现[J]. 电脑编程技巧与维护, 2017(11): 38-39+50.
|
|
[7]
|
陆兆攀, 刘萍萍, 卢颖. 高并发搜索系统下内存池的设计和实现[J]. 西安工业大学学报, 2016, 36(3): 187-193+205.
|
|
[8]
|
王显宏. 面向深度学习平台的内存管理器的设计与实现[D]: [硕士学位论文]. 哈尔滨: 哈尔滨工业大学, 2018.
|
|
[9]
|
栾添. 三维引擎中内存对象管理器的设计与实现[D]: [硕士学位论文]. 沈阳: 中国科学院大学(中国科学院沈阳计算技术研究所), 2018.
|
|
[10]
|
李灏江. 内存分配器对高并发数据库性能影响研究[J]. 电脑知识与技术, 2026, 22(2): 68-72.
|
|
[11]
|
欧阳湘臻, 朱怡安, 史先琛. 榫卯: 一种可组合的定制化内存分配框架[J]. 软件学报, 2024, 35(4): 2076-2098.
|
|
[12]
|
陈朝辉, 段雄. 基于TLSF算法的高效内存分配算法的设计与实现[J]. 计算机工程与科学, 2024, 46(11): 1916-1923.
|
|
[13]
|
王庆. 面向新型非易失性存储器的内存管理系统设计与实现[D]: [硕士学位论文]. 成都: 电子科技大学, 2016.
|
|
[14]
|
向翔宇. 具有崩溃一致性保证的高性能持久内存分配器[D]: [硕士学位论文]. 武汉: 华中科技大学, 2025.
|