NingG +

Linux下查看内存占用情况(C实现)

背景

时间:2012-10-18 14:21

目标:实现一个c函数,功能:在屏幕上输出当前内存使用情况,包括已用内存、空闲内存。 效果如图:shell命令,free –m

free-m

分析

  1. c的函数中,有没有现成的函数;
  2. shell中命令free –m的详解(自己man即可,-m表示结果以MB为单位显示)

经查询,linux下系统自带函数int sysinfo(struct sysinfo *info)可以实现此功能;

由于不同linux内核版本对应的struct sysinfo内部成员不同,在linux下,输入如下命令:

man sysinfo()

获得当前系统中sysinfo结构体的定义:

struct sysinfo {
		   long uptime;             /* Seconds since boot */
		   unsigned long loads[3];  /* 1, 5, and 15minute load averages */
		   unsigned long totalram;  /* Total usable main memory size */
		   unsigned long freeram;   /* Available memory size */
		   unsigned long sharedram; /* Amount of shared memory */
		   unsigned long bufferram; /* Memory used by buffers */
		   unsigned long totalswap; /* Total swap space size */
		   unsigned long freeswap;  /* swap space still available */
		   unsigned short procs;    /* Number of current processes */
		   unsigned long totalhigh; /* Total high memory size */
		   unsigned long freehigh;  /* Available high memory size */
		   unsigned int mem_unit;   /* Memory unit size in bytes */
		   char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
		   };

有了上面这些知识,正式开始动手写程序: 头文件:get_MEM.h

#ifndef GET_MEM_H
#define GET_MEM_H
 
/*********************
 *function: get the memory used and free in MB units.
 *return: 0 , success; other ,fail
 *********************
 */
int get_MEM(const char*);
#endif

主程序文件:get_MEM.c

#include stdio.h
#include stdlib.h
#include linux/kernel.h   /* 包含sysinfo结构体信息*/
#include string.h
#include "get_MEM.h"
 
int main(int argc, char *agrv[]){
	get_MEM("hello");
}
 
int get_MEM(const char* str){
	struct sysinfo s_info;
	int error = sysinfo(&s_info);
	if (error != 0){
		exit(EXIT_FAILURE);
	}
 
	printf("RAM(MB): used %u \t free %u \r  %s \n ",
			(unsigned int)((s_info.totalram-s_info.freeram)/(1024*1024.0)),
						(unsigned int)(s_info.freeram/(1024*1024.0)),  str);
	return 0;
}

将两个文件:get_MEM.hget_MEM.c放在同一路径下,使用gcc进行编译,并运行

gcc get_MEM.c
./a.out

运行效果如下:

RAM(MB):used 1377        free 10651      hello

原文地址:https://ningg.top/linux-memory-usage/
微信公众号 ningg, 联系我

同类文章:

微信搜索: 公众号 ningg, 联系我, 交个朋友.

Top