分配大块内存时,Hoard性能严重下降。

3

我用'C'语言编写了一个动态内存密集型的示例程序,并尝试对'glibc'默认分配器和Hoard分配器进行基准测试(以所需时间为标准)。

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3
  4 #define NUM_OF_BLOCKS   (1 * 4096)
  5
  6 void *allocated_mem_ptr_arr[NUM_OF_BLOCKS];
  7
  8 int
  9 main (int argc, char *argv[])
 10 {
 11     void *myblock = NULL;
 12
 13     int count, iter;
 14
 15     int blk_sz;
 16
 17     if (argc != 2)
 18     {
 19         fprintf (stderr, "Usage:./memory_intensive <Block size (KB)>\n\n");
 20         exit (-1);
 21     }
 22
 23     blk_sz = atoi (argv[1]);
 24
 25     for (iter = 0; iter < 1024; iter++)
 26     {
 27         /*
 28          * The allocated memory is not accessed (read/write) hence the residual memory
 29          * size remains low since no corresponding physical pages are being allocated
 30          */
 31         printf ("\nCurrently at iteration %d\n", iter);
 32         fflush (NULL);
 33
 34         for (count = 0; count < NUM_OF_BLOCKS; count++)
 35         {
 36             myblock = (void *) malloc (blk_sz * 1024);
 37             if (!myblock)
 38             {
 39                 printf ("malloc() fails\n");
 40                 sleep (30);
 41                 return;
 42             }
 43
 44             allocated_mem_ptr_arr[count] = myblock;
 45         }
 46
 47         for (count = 0; count < NUM_OF_BLOCKS; count++)
 48         {
 49             free (allocated_mem_ptr_arr[count]);
 50         }
 51     }
 52 }

由于此基准活动,我得到了以下结果(块大小,使用默认分配器的经过的时间,使用Hoard的经过的时间):
  1. '1K' '4.380秒' '0.927秒'
  2. '2k' '8.390秒' '0.960秒'
  3. '4k' '16.757秒' '1.078秒'
  4. '8k' '16.619秒' '1.154秒'
  5. '16k' '17.028秒' '13分6.463秒'
  6. '32k' '17.755秒' '5分45.039秒'
可以看出,对于块大小大于等于16K,Hoard的性能严重下降。这是什么原因?我们可以说Hoard不适用于分配大尺寸块的应用程序吗?

你有没有查看你提到的分配器的源代码?它们是自由软件。 - Basile Starynkevitch
请参阅此问题:http://stackoverflow.com/q/9204354/841108 - Basile Starynkevitch
1
我尝试复制您的测试,结果发现Hoard在每个大小上都优于标准分配器,特别是在较大的块大小上。例如,在32KB时,Hoard花费了11.22秒,而标准分配器则需要35.89秒。(Linux,x86-64,Hoard 3.8) - David Schwartz
是啊!行号让剪切/粘贴代码变得非常有趣 =P - WhozCraig
很明显,这里有些不对劲。我之前看了一下Hoard,但在我的机器上编译它时出了些问题,所以没有进展。 - Mats Petersson
1个回答

0

http://locklessinc.com/benchmarks_allocator.shtml上有一些不错的基准测试和解释:

对于小的分配,它仍然与tcmalloc表现类似。但是,在大约64KiB之后,性能急剧下降。它使用一个中央“储藏室”在线程之间重新分配内存。这会产生瓶颈,因为一次只能有一个线程在使用它。随着线程数量的增加,问题变得越来越严重。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接