2.内存使用率的计算:/proc/meminfo
前面写到了在如何获得cpu使用率的问题,解决了这个问题,我们再来解决内存使用率的问题,相对来说就比较简单了,这时候我们就用到了/proc/meminfo这个文件了。先看一下这个文件中都有什么东西吧!
cat /proc/meminfo
MemTotal: 1921916 kB
MemFree: 439180 kB
Buffers: 131680 kB
Cached: 736536 kB
SwapCached: 20404 kB
Active: 702404 kB
Inactive: 674888 kB
Active(anon): 415528 kB
Inactive(anon): 216500 kB
Active(file): 286876 kB
Inactive(file): 458388 kB
Unevictable: 0 kB
Mlocked: 0 kB
HighTotal: 1068680 kB
HighFree: 13536 kB
LowTotal: 853236 kB
LowFree: 425644 kB
SwapTotal: 7811068 kB
SwapFree: 7790572 kB
Dirty: 44 kB
Writeback: 0 kB
AnonPages: 493804 kB
Mapped: 107496 kB
Shmem: 122948 kB
Slab: 60624 kB
SReclaimable: 42576 kB
SUnreclaim: 18048 kB
KernelStack: 3048 kB
PageTables: 7496 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 8772024 kB
Committed_AS: 2533848 kB
VmallocTotal: 122880 kB
VmallocUsed: 28724 kB
VmallocChunk: 81896 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
HugePages_Total: 0
文件中有很多的项的内容,但是在计算利用率的时候我能用到的就是前两行的内容!
mem_rate = total - free / total
怎么样很简单吧!
#include
#include
#include
using namespace std;
typedef double T;
void readToVector(vector &tvec,ifstream &in)
{
string s;
T temp;
in>>s;
in>>temp;
tvec.push_back(temp);
in>>s;
in>>s;
in>>temp;
tvec.push_back(temp);
}
T calcMemRate(vector &tvec)
{
T rate ;
rate = 100 * (tvec[0] - tvec[1]) / tvec[0];
return rate;
}
int main()
{
vector tvec;
T temp;
ifstream in("/proc/meminfo");
if(!in)
{
cout<<"this file can not be opened!"< }
else
{
readToVector(tvec,in);
temp = calcMemRate(tvec);
cout<<"the memory using rate is:"< }
return 0;
}
阅读(2740) | 评论(0) | 转发(0) |