-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.cpp
37 lines (32 loc) · 1.07 KB
/
memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <unistd.h>
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void mem_usage(double& vm_usage, double& resident_set)
{
vm_usage = 0.0;
resident_set = 0.0;
ifstream stat_stream("/proc/self/stat", ios_base::in);
string pid, comm, state, ppid, pgrp, session, tty_nr;
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
string utime, stime, cutime, cstime, priority, nice;
string O, itrealvalue, starttime;
unsigned long vsize;
long rss;
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr >>
tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt >> utime >>
stime >> cutime >> cstime >> priority >> nice >> O >> itrealvalue >>
starttime >> vsize >> rss;
stat_stream.close();
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024;
vm_usage = vsize / 1024.0;
resident_set = rss * page_size_kb;
}
int main()
{
double vm, rss;
mem_usage(vm, rss);
cout << "Virtual Memory: " << vm << "\nResident set size: " << rss << endl;
}