Skip to content

Instantly share code, notes, and snippets.

@clevertension
Last active August 12, 2018 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clevertension/0f6ee7e0a122ed8b7b9b to your computer and use it in GitHub Desktop.
Save clevertension/0f6ee7e0a122ed8b7b9b to your computer and use it in GitHub Desktop.
get the peak vm
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
//getpid
#include <unistd.h>
////mkdir
#include <sys/stat.h>
////setvbuf
#include <cstdio>
////ptrace
#include <sys/ptrace.h>
////rusage
#include <sys/resource.h>
//
////wait4
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/user.h>
using namespace std;
int get_memory_usage(pid_t processPid) {
char buffer[64];
sprintf(buffer, "/proc/%d/status", processPid);
FILE* fproc = fopen(buffer, "r");
if (fproc == NULL) {
return -1;
}
int vmPeak = 0;
while (fgets(buffer, 32, fproc)) {
if (!strncmp(buffer, "VmPeak:", 7)) {
sscanf(buffer + 7, "%d", &vmPeak);
}
}
fclose(fproc);
return vmPeak;
}
int main() {
cout<<"vmpeak (KB) "<<get_memory_usage(getpid())<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment