wmic 中 获取系统内存使用

尝试了多种方法, 取得的系统的内存使用量总是和 windows 任务管理器 底部显示的 内存使用有较大差距.

用以下方法可以很接近任务管理器中的值.

由 wmic OS 中取得 FreeSpaceInPagingFiles, SizeStoredInPagingFiles

已用内存即为 SizeStoredInPagingFiles - FreeSpaceInPagingFiles

以下为程序实现


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值