-
Notifications
You must be signed in to change notification settings - Fork 3
/
MemoryMonitor.cpp
49 lines (40 loc) · 895 Bytes
/
MemoryMonitor.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
38
39
40
41
42
43
44
45
46
47
48
49
//
// Created by rohit on 6/28/17.
//
#include "MemoryMonitor.h"
#include <iostream>
#ifdef linux
#include <sys/resource.h>
#endif
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int parseLine(char *line) {
// This assumes that a digiti will be found and the line ends in " Kb".
int i = strlen(line);
const char *p = line;
while (*p < '0' || *p > '9') p++;
line[i - 3] = '\0';
i = atoi(p);
return i;
}
int getMem() {
int mem;
#ifdef linux
FILE *file = fopen("/proc/self/status", "r");
int result = -1;
char line[128];
while (fgets(line, 128, file) != NULL) {
if (strncmp(line, "VmRSS:", 6) == 0) {
result = parseLine(line);
break;
}
}
fclose(file);
mem= result / 1024;
#endif
#ifdef _WIN32
mem= 0;
#endif
return mem;
}