首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tcmalloc ReleaseFreeMemory()未正确释放内存

tcmalloc ReleaseFreeMemory()未正确释放内存
EN

Stack Overflow用户
提问于 2013-12-21 23:18:34
回答 1查看 5K关注 0票数 3

我在我的一个应用程序中使用tcmalloc,其中堆的增长和收缩非常大,显然我遇到了tcmalloc没有将内存释放回操作系统的问题。现在,我尝试通过MallocExtension::instance()->ReleaseFreeMemory();使用api来完成此操作。它工作得很好,释放了内存。但是,当我在一段时间(比如5分钟)后继续运行我的进程时,内存仍然会增加到初始水平(有时会更高)。奇怪的是,应用程序是空闲的。

以下是我的代码

代码语言:javascript
复制
#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   

感谢任何人的帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-02-27 13:57:44

您可以尝试包含malloc.h并将malloc_stats()包装在对MallocExtension::instance()->ReleaseFreeMemory(),的调用中,如下所示……

代码语言:javascript
复制
malloc_stats();
MallocExtension::instance()->ReleaseFreeMemory();
malloc_stats();

然后,您应该会看到类似以下内容:

之前:

代码语言:javascript
复制
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)

之后:

代码语言:javascript
复制
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)

如果没有其他情况,这将验证内存实际上是从页面堆空闲列表中释放的,并且现在是未映射的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20720430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档