61

I am trying to find out how much memory my application is consuming from within the program itself. The memory usage I am looking for is the number reported in the "Mem Usage" column on the Processes tab of Windows Task Manager.

Brian Stewart
  • 9,157
  • 11
  • 54
  • 66

5 Answers5

59

A good starting point would be GetProcessMemoryInfo, which reports various memory info about the specified process. You can pass GetCurrentProcess() as the process handle in order to get information about the calling process.

Probably the WorkingSetSize member of PROCESS_MEMORY_COUNTERS is the closest match to the Mem Usage coulmn in task manager, but it's not going to be exactly the same. I would experiment with the different values to find the one that's closest to your needs.

hmjd
  • 120,187
  • 20
  • 207
  • 252
Charlie
  • 44,214
  • 4
  • 43
  • 69
26

I think this is what you were looking for:

#include<windows.h>
#include<stdio.h>   
#include<tchar.h>

// Use to convert bytes to MB
#define DIV 1048576

// Use to convert bytes to MB
//#define DIV 1024

// Specify the width of the field in which to print the numbers. 
// The asterisk in the format specifier "%*I64d" takes an integer 
// argument and uses it to pad and right justify the number.

#define WIDTH 7

void _tmain()
{
  MEMORYSTATUSEX statex;

  statex.dwLength = sizeof (statex);

  GlobalMemoryStatusEx (&statex);


  _tprintf (TEXT("There is  %*ld percent of memory in use.\n"),WIDTH, statex.dwMemoryLoad);
  _tprintf (TEXT("There are %*I64d total Mbytes of physical memory.\n"),WIDTH,statex.ullTotalPhys/DIV);
  _tprintf (TEXT("There are %*I64d free Mbytes of physical memory.\n"),WIDTH, statex.ullAvailPhys/DIV);
  _tprintf (TEXT("There are %*I64d total Mbytes of paging file.\n"),WIDTH, statex.ullTotalPageFile/DIV);
  _tprintf (TEXT("There are %*I64d free Mbytes of paging file.\n"),WIDTH, statex.ullAvailPageFile/DIV);
  _tprintf (TEXT("There are %*I64d total Mbytes of virtual memory.\n"),WIDTH, statex.ullTotalVirtual/DIV);
  _tprintf (TEXT("There are %*I64d free Mbytes of virtual memory.\n"),WIDTH, statex.ullAvailVirtual/DIV);
  _tprintf (TEXT("There are %*I64d free Mbytes of extended memory.\n"),WIDTH, statex.ullAvailExtendedVirtual/DIV);


}
Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
Ronin
  • 2,027
  • 8
  • 32
  • 39
  • 8
    It probably isn't what he wanted to know, as this measure system memory in use, not memory consumed by an individual process. However it is may be useful to know too, so I won't down-mark it. – CashCow Sep 21 '12 at 11:25
  • 2
    Source: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366589%28v=vs.85%29.aspx – user956584 Jun 01 '13 at 21:56
  • This is not what question is about, alhough may be useful generally. – ivan.ukr Feb 08 '16 at 18:27
  • It does answer the question, but it could be more clear. Three of the fields returned pertain to the calling process, not to the system as you would expect. See answer below by @yunus-king and read API carefully. – trindflo Jun 20 '23 at 08:31
9

GetProcessMemoryInfo is the function you are looking for. The docs on MSDN will point you in the right direction. Get the info you want out of the PROCESS_MEMORY_COUNTERS structure you pass in.

You'll need to include psapi.h.

7

Try having a look at GetProcessMemoryInfo. I haven't used it, but it looks like what you need.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
7

To complement the answer by Ronin, indead the function GlobalMemoryStatusEx gives you the proper counters to derive the virtual memory usage for the calling process: just substract ullAvailVirtual from ullTotalVirtual to get allocated virtual memory. You can check this for your self using ProcessExplorer or something.

It is confusing that the system call GlobalMemoryStatusEx unfortunately has mixed purpose: it provides both system wide and process specific information, e.g. virtual memory information.

Yunus King
  • 1,141
  • 1
  • 11
  • 23
  • `GlobalMemoryStatusEx` doesn't give any information about the current process, only about the overall system. – Cosmin May 27 '21 at 15:49
  • 1
    @Cosmin, please have a detailed look at https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex. – Yunus King Jun 02 '21 at 08:40