VS malloc的自定义分配器:的malloc有很大的开销。为什么? [英] Malloc vs custom allocator: Malloc has a lot of overhead. Why?

查看:169
本文介绍了VS malloc的自定义分配器:的malloc有很大的开销。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,现在有两种不同版本的内存分配系统的图像融为一体pression应用。在原单,malloc的使用无处不在,而在第二个,我实现了一个简单的池分配器,刚分配的内存块,并返回内存myalloc的部分()调用。

I have an image compression application that now has two different versions of memory allocation systems. In the original one, malloc is used everywhere, and in the second one, I implemented a simple pool-allocator, that just allocates chunk of memory and returns parts of that memory to myalloc() calls.

我们已经注意到一个巨大的内存开销时使用的malloc:在它的内存使用情况的高度,的malloc()code需要一个1920x1080x16bpp图像约170兆内存,而池分配器刚分配48兆字节,其中47是由程序使用。

We've been noticing a huge memory overhead when malloc is used: At the height of its memory usage, the malloc() code requires about 170 megabytes of memory for a 1920x1080x16bpp image, while the pool allocator allocates just 48 megabytes, of which 47 are used by the program.

在的存储器分配模式而言,程序分配大量8字节的(最),32字节(多)和1080byte块(部分)与测试图像。除了这些,还有在code没有动态内存分配。

In terms of memory allocation patterns, the program allocates a lot of 8byte(most), 32-byte(many) and 1080byte-blocks(some) with the test image. Apart from these, there are no dynamic memory allocations in the code.

测试系统的操作系统是Windows 7(64位)。

The OS of the testing system is Windows 7 (64 Bit).

我们怎么测试内存使用情况?

How did we test memory usage?

通过自定义的分配器,我们可以看到有多少内存使用,因为所有的malloc调用延迟执行的分配器。使用malloc(),在调试模式下我们刚刚经历了code踩到,看着在任务管理器中的内存使用情况。在释放模式我们做相同,但少细粒度因为编译器优化了很多东西走,所以我们无法通过一块code片步骤(释放和调试的记忆差约为20MB,这是我将归因于优化和发布模式缺乏调试信息)。

With the custom allocator, we could see how much memory is used because all malloc calls are defered to the allocator. With malloc(), in Debug mode we just stepped through the code and watched the memory usage in the task manager. In release mode we did the same, but less fine grained because the compiler optimizes a lot of stuff away so we couldn't step through the code piece by piece (the memory difference between release and debug was about 20MB, which I would attribute to optimization and lack of debug information in release mode).

可以独自malloc的是这样一个巨大的开销的原因是什么?如果是这样,到底是什么导致里面的malloc开销?

Could malloc alone be the cause of such a huge overhead? If so, what exactly causes this overhead inside malloc?

推荐答案

首先,在所有的malloc对齐指针为16字节边界。此外,它们存储在地址$ P $至少有一个指针(或分配的长度)pceding返回值。然后,他们可能加魔法值或释放计数器,以表明链表不破,或内存块没有被释放两次(免费ASSERTS双释放的)。

First at all malloc aligns the pointers to 16 byte boundaries. Furthermore they store at least one pointer (or allocated length) in the addresses preceding the returned value. Then they probably add a magic value or release counter to indicate that the linked list is not broken or that the memory block has not been released twice (free ASSERTS for double frees).

#include <stdlib.h>
int main(int ac, char**av)
{
  int *foo = malloc(4);
  int *bar = malloc(4);
  printf("%d\n", (int)bar - (int)foo);
}

返回:32

这篇关于VS malloc的自定义分配器:的malloc有很大的开销。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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