如何避免堆碎片? [英] How to avoid heap fragmentation?
问题描述
我目前正在研究一个需要大量内存的医学图像处理项目.我可以做些什么来避免堆碎片并加快对已加载到内存中的图像数据的访问?
I'm currently working on a project for medical image processing, that needs a huge amount of memory. Is there anything I can do to avoid heap fragmentation and to speed up access of image data that has already been loaded into memory?
该应用程序使用 C++ 编写并在 Windows XP 上运行.
The application has been written in C++ and runs on Windows XP.
应用程序对图像数据进行一些预处理,例如重新格式化、计算查找表、提取感兴趣的子图像……应用程序在处理过程中需要大约 2 GB RAM,其中大约 1.5 GB 可用于图像数据.
The application does some preprocessing with the image data, like reformatting, calculating look-up-tables, extracting sub images of interest ... The application needs about 2 GB RAM during processing, of which about 1,5 GB may be used for the image data.
推荐答案
如果您在进行医学图像处理,则很可能一次分配大块(512x512,每像素 2 字节的图像).如果您在图像缓冲区分配之间分配较小的对象,那么碎片会咬你.
If you are doing medical image processing it is likely that you are allocating big blocks at a time (512x512, 2-byte per pixel images). Fragmentation will bite you if you allocate smaller objects between the allocations of image buffers.
对于这个特定的用例,编写自定义分配器不一定很难.您可以为 Image 对象使用标准的 C++ 分配器,但对于像素缓冲区,您可以使用在 Image 对象中全部管理的自定义分配.这是一个快速而肮脏的大纲:
Writing a custom allocator is not necessarily hard for this particular use-case. You can use the standard C++ allocator for your Image object, but for the pixel buffer you can use custom allocation that is all managed within your Image object. Here's a quick and dirty outline:
- 使用静态结构数组,每个结构具有:
- 可以容纳 N 个图像的可靠内存块 -- 分块将有助于控制碎片 -- 尝试将初始 N 设为 5 左右
- 一个平行的布尔数组,指示相应的图像是否在使用中
- 如果没有找到,则在数组末尾追加一个新结构
这只是一个简单的想法,有很大的变化空间.主要技巧是避免释放和重新分配图像像素缓冲区.
This is just one simple idea with lots of room for variation. The main trick is to avoid freeing and reallocating the image pixel buffers.
这篇关于如何避免堆碎片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!