c++ - 'malloc_stats' 函数中的奇怪行为

标签 c++ memory-management memory-leaks

我正在用“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;
}

这是我得到的输出:

./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

此时,如果我比较分配前和释放后的“in use bytes”,有784 字节的差异。

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

谢谢。

最佳答案

简答

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

长答案

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

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

./exe 1000 100 1

其中 1000 是 block 数,100 是 block 大小。由于 100 不是操作系统使用的页面大小的倍数,因此 malloc 往往会分配比您在每次调用时请求的更多的内存。

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

getconf PAGE_SIZE

在我的案例中返回 4096。当我运行你的程序时

./exe 100 4096 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     =     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

引用资料

关于c++ - 'malloc_stats' 函数中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53429866/

相关文章:

cocoa - OpenCV NSImage 到 IplImage 转换器过程中的泄漏

c++ - "Grow"动态数组函数产生错误

xcode - 如何在 macOS 下模拟内存不足的情况?

C程序通过Valgrind检查: can't return all the reserved storage space

javascript - 如何计算在 Javascript 中 for 循环可以运行的最大次数?

c++ - 这是内存泄漏吗? (C++ BSTR)

memory-leaks - jemalloc 堆分析仅跟踪分配吗?

c++ - 自定义刷新实现

c++ - 从主进程执行多个进程

c++ - BIO_do_connect() 似乎失败是因为使用了 SSL v3,有没有办法获得更多诊断信息?