Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:02:03+00:00 2026-06-03T21:02:03+00:00

I would like to get the total memory allocated before and after I call

  • 0

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.

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.

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++;

With a result like this:

after.PrivateUsage - before.PrivateUsage = 12288

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:02:05+00:00Added an answer on June 3, 2026 at 9:02 pm

    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.

    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__)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a facebook app that I would like to get the total number
I would like to get the total memory of a host by using a
I have this form but would like to get the total. Please how can
I would like to know if it is possible to get the total miles
I'd like to know the total amount of memory allocated while a method runs.
I would like to get some stats programmatically about my app; for example: total
I would like to get the total sum of the results in Stocks, ETFs
I would like to get a different solution to a problem I have solved
I have a listview and would like to calculate a running total of values.
I would like to get share, like, comment and total count from a fan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.