ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

c – API调用获取进程的当前堆大小?

2019-08-23 23:00:33  阅读:167  来源: 互联网

标签:c-3 c linux heap-memory


我在大型C应用程序中调试缓慢的内存泄漏,我想在程序的各个点打印出当前的堆大小.

除了打开和解析/ proc / PID / statm之外是否有任何库API调用,我可以从中获取此信息?

然而,有一篇文章建议sbrk()返回当前的堆指针 – 而不是我想要的100%. (第二个问题:sbrk()值的变化是否与当前堆大小的变化相对应?)

我看了,但似乎很奇怪没有系统调用……

谢谢

更新我

我在调用sbrk()和读取proc /…/ statm之间做了一些测试比较.似乎sbrk()不反映实际分配.相反,似乎statm测量实际分配,而sbrk()显示总堆大小.

这个总大小的增量大块(等于页面大小?).

下面的测试程序产生了这个输出(由sbrk()报告的堆大小和/ proc /…/statm报告的内存使用情况,清楚地显示了一个区别:

0 ALLOC: HEAP SIZE: 0
MEMORY USAGE: 1308 201 174 2 0 566 0
1 ALLOC: HEAP SIZE: 135168
MEMORY USAGE: 1565 212 184 2 0 823 0
2 ALLOC: HEAP SIZE: 135168
MEMORY USAGE: 1822 216 187 2 0 1080 0
3 ALLOC: HEAP SIZE: 135168
MEMORY USAGE: 2079 217 187 2 0 1337 0
4 ALLOC: HEAP SIZE: 135168
MEMORY USAGE: 2336 218 187 2 0 1594 0
5 ALLOC: HEAP SIZE: 135168
MEMORY USAGE: 2593 219 187 2 0 1851 0

0 FREE: HEAP SIZE: 135168
MEMORY USAGE: 3364 225 189 2 0 2622 0
1 FREE: HEAP SIZE: 135168
MEMORY USAGE: 3107 224 189 2 0 2365 0
2 FREE: HEAP SIZE: 135168
MEMORY USAGE: 2850 223 189 2 0 2108 0
3 FREE: HEAP SIZE: 135168
MEMORY USAGE: 2593 222 189 2 0 1851 0
4 FREE: HEAP SIZE: 135168
MEMORY USAGE: 2336 221 189 2 0 1594 0
5 FREE: HEAP SIZE: 135168
MEMORY USAGE: 2079 220 189 2 0 1337 0

测试程序

class CfgProfileList
{
public:
    bool obtainSystemProfileList();
    void leakObjTest();
    std::set<std::string> mProfileList;
private:
    char dummy[1024 * 1024]; // use up some space
};

class ComUtil
{
public:
    static void printMemoryUsage();
private:
    static unsigned int mHeapOrigin;
};

/* static */
unsigned int ComUtil::mHeapOrigin = 0;

// Print current process memory utilization
/* static */ void
ComUtil::printMemoryUsage()
{
    unsigned int pHeap = (unsigned int)sbrk(0);
    if (mHeapOrigin == 0)
        mHeapOrigin = pHeap;

    printf("HEAP SIZE: %u\n", pHeap - mHeapOrigin);

    char fname[256], line[256];
    sprintf(fname, "/proc/%d/statm", getpid());

    FILE *pFile = fopen(fname, "r");
    if (!pFile)
        return;    
    fgets(line, 255, pFile);
    fclose(pFile);
    printf("MEMORY USAGE: %s", line);
}   

void
CfgProfileList::leakObjTest()
{
    CfgProfileList *pointerList[50];
    int  n = 10;
    int  sleep = 100000;

    printf("OBJECT ALLOCATION\n");    
    for (int i = 0; i < n; i++)
    {
        pointerList[i] = new CfgProfileList;
        printf("%d ALLOC: ", i);
        ComUtil::printMemoryUsage();
        usleep(sleep);
    }

    printf("\n");

    for (int i = 0; i < n; i++)
    {
        delete pointerList[i];
        printf("%d FREE: ", i);
        ComUtil::printMemoryUsage();
        usleep(sleep);
    }
}

int
main(int argc, char **argv)
{
    CfgProfileList pl;
    pl.leakObjTest();
}

解决方法:

由于glibc的new基于malloc(),因此可以使用malloc信息和调试功能;例如,您可以向应用程序添加malloc_stats()的调用.

#include <malloc.h>
…
    malloc_stats();

The malloc_stats() function prints (on standard error) statistics about memory allocated by
malloc(3) and related functions. …

你可能也看看

> mallinfo(),

The mallinfo() function returns a copy of a structure containing information about memory allocations performed by malloc(3) and related
functions. …

> malloc_hook,

The GNU C library lets you modify the behavior of malloc(3), realloc(3), and
free(3) by specifying appropriate hook functions. You can use these hooks to help you debug programs that use dynamic memory allocation, for
example.

>和mtrace().

The mtrace() function installs hook functions for the memory-allocation functions (malloc(3), realloc(3) memalign(3),
free(3)). These hook functions record tracing information about memory allocation and deallocation. The tracing information can be used to discover
memory leaks and attempts to free nonallocated memory in a program.

标签:c-3,c,linux,heap-memory
来源: https://codeday.me/bug/20190823/1702128.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有