我在我的一个应用程序中使用 tcmalloc,其中堆的增长和收缩量非常大,显然我遇到了 tcmalloc 没有将内存释放回操作系统的问题。现在我尝试使用 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
感谢任何帮助。