I tried multiple methods, and the system's memory usage is always quite different from the memory usage shown at the bottom of the Windows Task Manager.
Use the following method to get close to the value in the task manager.
Freespaceinpagingfiles and sizestoredinpagingfiles are obtained from wmic OS.
Used memory is sizestoredinpagingfiles-freespaceinpagingfiles
The program implementation is as follows:
/* * Author: xiaomu * Date: 2012/04/11 */#include <stdio.h>#include <stdlib.h>int main(){FILE *free, *total;int free_size, total_size;char *cmd_free = "wmic OS get freespaceinpagingfiles|findstr /v /i \"freespaceinpagingfiles\"|more > free";char *cmd_total = "wmic OS get SizeStoredInPagingFiles|findstr /v /i \"SizeStoredInPagingFiles\"|more >total";system(cmd_free);system(cmd_total);total = fopen("total", "r");if(total == NULL){perror("fopen failed");return -1;}free = fopen("free", "r");if(free == NULL){perror("fopen failed");return -1;}fscanf(free, "%d", &free_size);fscanf(total, "%d", &total_size);printf("total: %dm ", total_size/1024);printf("used: %dm\n", (total_size-free_size)/1024);fclose(free);fclose(total);while(1){system(cmd_free);free = fopen("free", "r");if(free == NULL){perror("fopen failed");return -1;}fscanf(free, "%d", &free_size);printf("total: %dm ", total_size/1024);printf("used: %dm\n", (total_size-free_size)/1024);fclose(free);}return 0;}