[英]How to grab total physical memory from Kernel
我正在使用linux/mm.h
struct sysinfo mem_info;
然后totalMemory = mem_info.totalram;
这给了我设备多少内存。 现在,如何获取正在使用的内存量? 我真的很讨厌它必须经历每个运行的过程并计算所使用的ram的总和。
遵循普通Linux实用程序的最基本方法是,将/proc/meminfo
文件作为文本打开并进行解析。
这是busybox实施的示例:
/*
* Revert to manual parsing, which incidentally already has the
* sizes in kilobytes. This should be safe for both 2.4 and
* 2.6.
*/
fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
/*
* MemShared: is no longer present in 2.6. Report this as 0,
* to maintain consistent behavior with normal procps.
*/
if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
shared = 0;
fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
fscanf(fp, "Cached: %lu %s\n", &cached, buf);
此处的第535行: http : //code.metager.de/source/xref/busybox/procps/top.c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.