Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to display detailed information about the system memory usage in C Win32 API

2 Answers

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

// Convert bytes to MB
#define DIV 1048576

// Specifies the width for right-justifying the output.
#define WIDTH 10 

int main()
{
    // A structure containing information about the system's memory usage, including physical, virtual, and paging files.
    MEMORYSTATUSEX statex;

    // initializes the structure and ensures the correct memory size is passed to the API function.
    statex.dwLength = sizeof(statex);

    // fetches the memory-related information and populates the statex structure.
    GlobalMemoryStatusEx(&statex);

    // The asterisk in: "%*I64d" takes an integer argument and uses it to pad and right justify the number
    // "%*I64d" pads the number with spaces to occupy WIDTH characters, aligning it to the right.

    /*
    dwMemoryLoad: Percentage of memory currently in use.
    ullTotalPhys: Total physical memory available on the system.
    ullAvailPhys: Free physical memory available.
    ullTotalPageFile: Total size of the paging file.
    ullAvailPageFile: Free size of the paging file.
    ullTotalVirtual: Total size of the virtual memory.
    ullAvailVirtual: Free size of the virtual memory.
    ullAvailExtendedVirtual: Free size of extended virtual memory (rarely used).
    */

    // Display memory details in MB
    _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);

    return 0;
}



/*
run:

There is          93 percent of memory in use.
There are      32701 total Mbytes of physical memory.
There are       2105 free Mbytes of physical memory.
There are     131005 total Mbytes of paging file.
There are      29604 free Mbytes of paging file.
There are  134217727 total Mbytes of virtual memory.
There are  134213583 free Mbytes of virtual memory.
There are          0 free Mbytes of extended memory.

*/

 



answered Mar 29, 2025 by avibootz
edited Mar 30, 2025 by avibootz
0 votes
#include <windows.h>
#include <stdio.h>   
#include <tchar.h>

// Convert bytes to GB
#define DIV 1073741824

// Specifies the width for right-justifying the output.
#define WIDTH 7 

int main()
{
    // A structure containing information about the system's memory usage, including physical, virtual, and paging files.
    MEMORYSTATUSEX statex;

    // initializes the structure and ensures the correct memory size is passed to the API function.
    statex.dwLength = sizeof(statex);

    // fetches the memory-related information and populates the statex structure.
    GlobalMemoryStatusEx(&statex);

    // The asterisk in: "%*I64d" takes an integer argument and uses it to pad and right justify the number
    // "%*I64d" pads the number with spaces to occupy WIDTH characters, aligning it to the right.

    /*
    dwMemoryLoad: Percentage of memory currently in use.
    ullTotalPhys: Total physical memory available on the system.
    ullAvailPhys: Free physical memory available.
    ullTotalPageFile: Total size of the paging file.
    ullAvailPageFile: Free size of the paging file.
    ullTotalVirtual: Total size of the virtual memory.
    ullAvailVirtual: Free size of the virtual memory.
    ullAvailExtendedVirtual: Free size of extended virtual memory (rarely used).
    */

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

    return 0;
}



/*
run:

There is       87 percent of memory in use.
There are      31 total Gbytes of physical memory.
There are       3 free Gbytes of physical memory.
There are     127 total Gbytes of paging file.
There are      43 free Gbytes of paging file.
There are  131071 total Gbytes of virtual memory.
There are  131067 free Gbytes of virtual memory.
There are       0 free Gbytes of extended memory.

*/

 



answered Mar 30, 2025 by avibootz
...