3

ヒープが非常に大量に拡大および縮小するアプリケーションの 1 つで tcmalloc を使用しています。明らかに、tcmalloc がメモリを OS に解放しないという問題に直面しました。今、私は api を使用してそれを実行しようとしましたMallocExtension::instance()->ReleaseFreeMemory();。正常に動作し、メモリを解放しました。しかし、しばらくの間 (たとえば 5 分) プロセスを実行し続けると、メモリはまだ初期レベル (場合によってはそれ以上) まで増加しています。奇妙なことに、アプリケーションはアイドル状態です。

これが私のコードです

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "google/malloc_extension.h"

int main(int argc, char* argv[])
{

    char** names;
    printf("\nBefore starting the execution. Press enter to start.... \n");
    getchar();
    if (argc < 3)
    {
        printf("Usage: ./a.out <numTimes> <allocsize>\n");
        exit(1);
    }
    int numTimes = atoi(argv[1]);
    int allocSize = atoi(argv[2]);
    names = (char**) malloc(numTimes * sizeof(char*));
    for (int i = 0; i < numTimes; i++)
    {
        names[i] = (char*)malloc(allocSize);
    }
    printf("\nDone with the execution. Press enter to free the memory.... \n");
    getchar();
    for (int i = 0; i < numTimes; i++)
    {
        free(names[i]);
    }
    free(names);
    printf("\nDone with the freeing. Press enter to release the memory.... \n");
    getchar();
    MallocExtension::instance()->ReleaseFreeMemory();
    printf("\nDone with the execution. Press enter to exit.... \n");
    getchar();
    return 0;
}



./a.out 10000 30000

after release

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
18823 sarath    20   0  332m 4568 1268 S  0.0  0.2   0:00.05 a.out  

after sometimes(4-5 mins)

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
18823 sarath    20   0  332m 129m 1268 S  0.0  6.5   0:00.05 a.out   

どんな助けにも感謝します。

4

1 に答える 1

3

malloc.h を含めて、mallocExtension::instance()->ReleaseFreeMemory() への呼び出しの周りに malloc_stats() をラップしてみてください。

malloc_stats();
MallocExtension::instance()->ReleaseFreeMemory();
malloc_stats();

次に、次のように表示されます。

前:

4997120 (    4.8 MiB) Bytes in page heap freelist
7434392 (    7.1 MiB) Actual memory used (physical + swap)
0 (    0.0 MiB) Bytes released to OS (aka unmapped)

後:

0 (    0.0 MiB) Bytes in page heap freelist
2437272 (    2.3 MiB) Actual memory used (physical + swap)
4997120 (    4.8 MiB) Bytes released to OS (aka unmapped)

他に何もない場合、メモリが実際にページ ヒープ フリーリストから解放され、現在マップされていないことが確認されます。

于 2014-02-27T05:57:44.170 に答える