WMIC to obtain system memory usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.