“malloc_stats”函数中的奇怪行为

Alb*_*ero 4 c++ memory-leaks memory-management

我正在用 'malloc_stats' 函数做一些测试,我看到了一个我不明白的奇怪行为。测试非常简单,我正在做的是分配内存并在分配之前,分配之后和释放内存之后打印'malloc_stats'。这是我正在使用的代码:

int main(int argc, char *argv[])
{
   char *alloc[MAX_ALLOCS];
   if ( argc < 3 or strcmp(argv[1], "--help") == 0 ) {
       cout << argv[0] << " num-blocks block-size [free-step [start-free [end-free]]]" << endl;
        return 1;
    }

   int numBlocks = atoi(argv[1]);
   size_t blockSize = atoi(argv[2]);
   int freeStep = (argc > 3) ? atoi(argv[3]) : 1;
   int freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
   int freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;

   cout << "============== Before allocating blocks ==============" << endl;
   malloc_stats();   

   for (int j = 0; j < numBlocks; j++)
   {
       alloc[j] = (char*) malloc(blockSize);
       if (alloc[j] == NULL) {
           cout << "ERROR: malloc" << endl;
           return 1;
       }
   }

   cout << endl << "============== After allocating blocks ==============" << endl;
   malloc_stats();   

   for (int j = freeBegin; j < freeEnd; j += freeStep) {
       free(alloc[j]);
   }

   cout << endl << "============== After freeing blocks ==============" << endl;
   malloc_stats();   

   return 1;
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

./exe 1000 100 1
============== Before allocating blocks ==============
Arena 0:
system bytes     =     135168
in use bytes     =      74352
Total (incl. mmap):
system bytes     =     135168
in use bytes     =      74352
max mmap regions =          0
max mmap bytes   =          0

============== After allocating blocks ==============
Arena 0:
system bytes     =     270336
in use bytes     =     186352
Total (incl. mmap):
system bytes     =     270336
in use bytes     =     186352
max mmap regions =          0
max mmap bytes   =          0

============== After freeing blocks ==============
Arena 0:
system bytes     =     270336
in use bytes     =      75136
Total (incl. mmap):
system bytes     =     270336
in use bytes     =      75136
max mmap regions =          0
max mmap bytes   =          0
Run Code Online (Sandbox Code Playgroud)

此时,如果我比较分配前和释放后的“使用中字节”,则相差784 个字节。

我无法理解发生了什么,我认为“使用中的字节”必须相同......这些字节在哪里?

谢谢你。

eoz*_*ozd 6

简答

这种字节数差异与操作系统使用的分页有关,而不是内存泄漏。当您分配的块大小不是页面大小的倍数时,不会释放一些已分配的额外内存。分配器稍后可以使用这些额外的内存,并且不会泄漏。

长答案

如果我们查看malloc_stats函数 [1]的手册页,我们会看到使用中分配消耗的总字节数是通过uordblksmallinfo 结构体 [2]的字段获得的,其文档说明这uordblks是总分配字节数. 在这里,已分配并不一定意味着您作为应用程序员使用了所有这些内存。如果分配器可以带来更高的性能和效率,则分配器可能会分配比出于对齐目的而请求的内存更多的内存。

这是您的问题中描述的问题。你运行你的程序

./exe 1000 100 1
Run Code Online (Sandbox Code Playgroud)

其中 1000 是块数,100 是块大小。由于 100 不是操作系统使用的页面大小的倍数,因此 malloc 将倾向于分配比每次调用请求更多的内存。

为了快速检查是否确实如此,我们可以分配与操作系统页面大小相等的块大小。在 Linux 发行版上,可以通过以下 shell 命令获取页面大小:

getconf PAGE_SIZE
Run Code Online (Sandbox Code Playgroud)

在我的情况下返回 4096。当我运行你的程序时

./exe 100 4096 1
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:(请注意,释放完成后使用中的字节与分配前使用中的字节相同)

============== Before allocating blocks ==============
Arena 0:
system bytes     =     135168
in use bytes     =      74352
Total (incl. mmap):
system bytes     =     135168
in use bytes     =      74352
max mmap regions =          0
max mmap bytes   =          0

============== After allocating blocks ==============
Arena 0:
system bytes     =     540672
in use bytes     =     485552
Total (incl. mmap):
system bytes     =     540672
in use bytes     =     485552
max mmap regions =          0
max mmap bytes   =          0

============== After freeing blocks ==============
Arena 0:
system bytes     =     208896
in use bytes     =      74352
Total (incl. mmap):
system bytes     =     208896
in use bytes     =      74352
max mmap regions =          0
max mmap bytes   =          0
Run Code Online (Sandbox Code Playgroud)

参考