2

数日以来、修正できないアプリケーションの 1 つのテストに行き詰まっています。だから私は自分のテストのためにテストを書くことにしました...

私はメモリを割り当て、使用中のメモリの成長を監視し、メモリを再度解放したいと考えています。割り当てられたメモリはもう使用されていないと思いますよね?

私は 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. なぜ 2 番目のテストは 4096 で 0 ではないのですか? 2. Forth テストが 0 でないのはなぜですか?

4回目のテストは私をびっくりさせます。ご覧ください。無料のメモリ呼び出しをテストする方法を説明できるかもしれません。メモリ マネージャはバイトを強制終了せずに再利用すると想定しているため、メモリが rus.ru_maxrss に感染します。しかし、どうすれば無料でテストできますか?

どうもありがとう、ピーター

4

1 に答える 1

1

で割り当てられたメモリは、 'dのmalloc場合でも、OSに戻されません。freeプログラムが終了するまで、メモリはOSに戻されません。

getrusageOSによって測定された、プログラムで使用されているメモリの量を返します。

上記の情報を組み合わせると、両方の質問に対する答えが得られます。

于 2012-04-05T09:37:01.727 に答える