如何避免堆碎片化? [英] How to avoid heap fragmentation?

查看:175
本文介绍了如何避免堆碎片化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事医学图像处理项目,该项目需要大量内存.我能做些什么来避免堆碎片并加快对已经加载到内存中的图像数据的访问?

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左右
    • bool的并行数组,指示是否正在使用相应的图像
    • Use a static array of structs, each struct has:
      • A solid chunk of memory that can hold N images -- the chunking will help control fragmentation -- try an initial N of 5 or so
      • A parallel array of bools indicating whether the corresponding image is in use
      • 如果未找到任何内容,则将新结构附加到数组的末尾

      这只是一个简单的想法,有很大的变化空间.主要技巧是避免释放和重新分配图像像素缓冲区.

      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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆