用C GetTotalMemory分配 [英] GetTotalMemory allocation in C

查看:153
本文介绍了用C GetTotalMemory分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得前总内存分配后,我打电话,为了一个函数来确定如果我正确与否释放一切。

I would like to get the total memory allocated before and after I call a function in order to determine if I have freed everything correctly or not.

我用C这样做的,我很生疏所以请原谅我,如果这是一个幼稚的问题。我在寻找类似C#GC.GetTotalMemory(真)的东西,这是在Windows现在。

I'm doing this in C and I'm very rusty so forgive me if this is a naive question. I'm looking for something similar to the C# GC.GetTotalMemory(true) and this is in Windows for now.

现在我使用 PROCESS_MEMORY_COUNTERS_EX GetProcessMemoryInfo(...),前后调用该函数之后,但因为如果我进入功能和呼叫注释掉我不能让元首或输出的尾巴免费(...)然后它会给我同样的结果(是后总是大)。以下是我现在所拥有的......

Right now I am using PROCESS_MEMORY_COUNTERS_EX and GetProcessMemoryInfo(...), before and after calling the function but I can't make heads or tails of the output because if I go into the function and comment out a call to free(...) then it will give me the same results (after is always larger). Here is what I have right now...

GetProcessMemoryInfo(hProc, &before, sizeof(before));
r = c->function();
GetProcessMemoryInfo(hProc, &after, sizeof(after));

if(r->result != 0) {
    printf("error: %s\r\n", c->name);
    printf("  %s\r\n", r->message);
    printf("  %s (%d)\r\n", r->file, r->line);
    failed++;
}
else if(after.PrivateUsage > before.PrivateUsage) {
    printf("memory leak: %s\r\n", c->name);
    printf("  %d kb\r\n", after.PrivateUsage - before.PrivateUsage);
    failed++;
}
else succeeded++;

通过这样的结果是:

after.PrivateUsage - before.PrivateUsage = 12288

如果我去注释掉一些调用来释放我得到了相同的结果。我怎样才能真正确定,我已经使用malloc分配的内存的当前总大小?

If I go and comment out some calls to free I get the same result. How can I actually determine the current total size of memory that I have allocated using malloc?

推荐答案

我不知道任何C标准库函数,它可以帮助你实现这一目标。据我所知,没有任何不存在的。但是你可以使用某种的黑客,一个人不应该使用这个在发布版本但是对于调试和诊断仅目的。

I am, not aware of any c standard library functions which can help you achieve this. AFAIK, there don't exist any. But you can use some sort of an hack, One should not use this in release builds but for debug and diagnostic purpose only.

您可以使用我称之为的malloc超载在C 。为此,您可以用宏的帮助。结果
你可以写在的malloc 的包装功能,而无需修改每个实例的code,其中的函数被调用,一个简单的宏就足够了:

You can use what I call malloc overloading in c. You do this with the help of macros.
You can write an wrapper function over malloc, without modifying each and every instance in your code where the function is called then a simple macro shall suffice:

void* my_malloc(size_t size, const char *file, int line, const char *func)
{

    void *p = malloc(size);
    printf ("Allocated = %s, %i, %s, %p[%li]\n", file, line, func, p, size);

    /*Link List functionality goes in here*/

    return p;
}

#define malloc(X) my_malloc( X, __FILE__, __LINE__, __FUNCTION__)

在自己的功能,您可以收集一些全局数据结构中的诊断,说一个链表。
对于例如:它可以维持,返回缓冲区地址,对于相应的配置等结果的大小
同样,你重载免费(),你可以做在那里,在传递给函数每个缓冲区地址可以对列表中的项目进行检查,并从名单中删除簿记当找到一个匹配。结果
在节目结束时,列表中包含已分配但永远不会被释放,又名内存泄漏内存条目。你可以提供一些API来从任何需要的时候保持列表的诊断信息。

Inside your own function you can collect the diagnostics in some global data structure, say a linked list. For ex: It can maintain, buffer address being returned, the size for that corresponding allocation etc.
Similarly, you overload free() and you can do the bookkeeping in there, Each buffer address passed in to the function can be checked against the list items and removed from the list as and when a match is found.
At the end of the program the list contains entries for memory that was allocated but never freed, aka memory leaks. You can provide some api to fetch the diagnostic details from the list maintained whenever required.

您可以经常用这种伎俩来编写自己的内存泄漏检测仪等,进行调试。

You can often use this trick to write your own memory leak detector etc, for debugging purposes.

这篇关于用C GetTotalMemory分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆