c - C 中的内存分配测试 - 如何为 malloc 和 free 编写 CUnit 测试?

标签 c testing malloc free cunit

几天以来,我无法修复我的一个应用程序的测试。所以我决定为我的测试写一个测试......

我想分配内存,观察正在使用的内存的增长,然后再次释放内存。我希望分配的内存不再使用,对吧?

我正在使用 Mac OSX (10.6.8) 和 gcc (GCC) 4.2.1

所以我写了这个测试:

void testMemoryFreeCheck(){
    puts("- test Memory Free Check  -");
    puts("- ----------------------- -");
    puts("- This test takes a while -");
    puts("");
    struct rusage rus;
    getrusage(RUSAGE_SELF, &rus);

    // Things befor we want to mature. 
    char *holder[10000];
    char *bigHolder;
    int loops =  10000;
    int i;

    // Variables used for tests. 
    long int afterFirstTestKiloByte = 0;
    long int afterSecondTestKiloByte = 0;
    long int afterThirdTestKiloByte = 0;
    long int afterForuthTestKiloByte = 0;

    getrusage(RUSAGE_SELF, &rus);
    long int initialKiloByte = rus.ru_maxrss;
    printf("Initial KB Used: %ld\n", initialKiloByte);

    // a adder to proof that we get it write
    char add = "A";
    getrusage(RUSAGE_SELF, &rus);
    long int afterAddKiloByte = rus.ru_maxrss;
    printf("After a char add KB Used: %ld\n", initialKiloByte);
    getrusage(RUSAGE_SELF, &rus);
    long int growingKiloByte = rus.ru_maxrss;

    // This loop should do nothing.
    for(i=0; i<loops; ++i){
        printf(".");
    }
    puts("");
    getrusage(RUSAGE_SELF, &rus);
    afterFirstTestKiloByte = rus.ru_maxrss;
    printf("First Test should be 0 : %ld\n", (afterFirstTestKiloByte - afterAddKiloByte));
    CU_ASSERT_TRUE( (afterFirstTestKiloByte - afterAddKiloByte) == 0);

    // This loop should free all allocated space.
    for(i=0; i<(loops * 128); ++i){
        char *tmp = malloc(1024 * 1024);
        free(tmp);
    }

    getrusage(RUSAGE_SELF, &rus);
    afterSecondTestKiloByte = rus.ru_maxrss;
    printf("Second Test should be 0 (4096 is ok for some reasons): %ld\n", (afterSecondTestKiloByte - afterAddKiloByte));
    CU_ASSERT_TRUE( (afterSecondTestKiloByte - afterAddKiloByte) <= 4096);

    // This loop should increase the allocated space.
    for(i=0; i<loops; ++i){
        holder[i] = malloc(1024 * 1024);
    }
    getrusage(RUSAGE_SELF, &rus);
    afterThirdTestKiloByte = rus.ru_maxrss;
    printf("Third Test should be grater than 0 : %ld\n", (afterThirdTestKiloByte - afterAddKiloByte));
    CU_ASSERT_TRUE( (afterThirdTestKiloByte - afterAddKiloByte) > 0);

    // now free the memmory and get back to the initial value
    for(i=0; i<loops; ++i){
        free(holder[i]);
    }  
    getrusage(RUSAGE_SELF, &rus);
    afterForuthTestKiloByte = rus.ru_maxrss;
    printf("Forth Test should be reset to 0 : %ld\n", (afterForuthTestKiloByte - afterAddKiloByte));
    CU_ASSERT_TRUE( (afterThirdTestKiloByte - afterAddKiloByte) == 0);

    puts("- ----------------------- -");
}

我的输出是:

- test Memory Free Check  -
- ----------------------- -
- This test takes a while -

Initial KB Used: 458752
After a char add KB Used: 458752
[...]
First Test should be 0 : 0
Second Test should be 0 (4096 is ok for some reasons): 4096
Third Test should be grater than 0 : 1601536
Forth Test should be reset to 0 : 1601536
- ----------------------- -

我现在有两个问题无法解释: 1.为什么第二次Test是4096而不是0? 2.为什么第四次测试没有0?

第四个测试吓坏了我。请看一看,也许您可​​以解释如何测试空闲内存调用。我假设内存管理器不会杀死字节,而是重用它,所以内存会感染 rus.ru_maxrss。 但是如何免费测试呢?

非常感谢, 彼得

最佳答案

使用 malloc 分配的内存不会返回给操作系统,即使在 free 时也是如此。在程序退出之前,内存不会返回给操作系统。

getrusage 返回操作系统测量的程序使用的内存量。

结合以上信息,您就得到了这两个问题的答案。

关于c - C 中的内存分配测试 - 如何为 malloc 和 free 编写 CUnit 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10026121/

相关文章:

c - 如何保存栈和堆

testing - 通过 QTP/UFT 使用 abs_x/abs_y 或 x/y(相对)进行 WebElement 识别有多可靠?

haskell - 在 Hedgehog 中通过 'Gen' 或通过 'forAll' 生成随机输入的区别

testing - 基于规范或需求的测试的覆盖率指标是什么?

C,在结构[]中分配一个数组

c - 将 C 翻译成 Golang。如何分配内存以匹配 C?

c - 如何在 OpenGL 中使用嵌套循环用较小的正方形列表填充平面?

c++ - 为什么文件扩展名对编译有影响?

c - C 中的 malloc() 内存损坏

c - 为乘法动态分配矩阵 C