繁体   English   中英

如何从 C++ 程序内部测量内存使用情况?

[英]How to measure memory usage from inside a C++ program?

对于 C++ 程序,是否有可能跟踪该程序一次使用了多少内存?

例如,一个带有原型的函数:

int getEstimatedTotalMemoryUsage();

我想如果这是不可能的,那么人们将不得不退出程序,进行系统调用并从那里检查结果。 如果是这样,有哪些工具可用于此类目的? 假设这样的事情是可能的,那就是。

编辑:我正在使用 linux,有什么工具可以为您做到这一点?

是 - 使用 POSIX getrusage Linux 手册页

概要

#include <sys/time.h> #include <sys/resource.h> int getrusage(int who, struct rusage *usage);

描述

getrusage()返回当前的资源使用,对于无论是RUSAGE_SELFRUSAGE_CHILDREN 前者请求当前进程使用的资源,后者请求已终止并等待的子进程使用的资源。

 struct rusage { struct timeval ru_utime; /* user time used */ struct timeval ru_stime; /* system time used */ long ru_maxrss; /* maximum resident set size */ long ru_ixrss; /* integral shared memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims */ long ru_majflt; /* page faults */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* messages sent */ long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ };

这是在 Windows 上测量进程使用的内存的示例。

#include <windows.h>
#include <Psapi.h>

// [...]

PROCESS_MEMORY_COUNTERS memCounter;
BOOL result = K32GetProcessMemoryInfo(GetCurrentProcess(), &memCounter, sizeof(memCounter));
std::cout << "WorkingSetSize " << memCounter.WorkingSetSize << std::endl;

以及返回值的解释https://docs.microsoft.com/en-gb/windows/win32/api/psapi/ns-psapi-process_memory_counters

我自己今天想要这个,所以在这里分享测试结果。 我相信在任何 unix 机器上调用 getmem() 都会执行 OP 的要求。 用非常通用的 C 编写,它可以在 C 或 C++ 中工作。

// Calling function must free the returned result.
char* exec(const char* command) {
  FILE* fp;
  char* line = NULL;
  // Following initialization is equivalent to char* result = ""; and just
  // initializes result to an empty string, only it works with
  // -Werror=write-strings and is so much less clear.
  char* result = (char*) calloc(1, 1);
  size_t len = 0;

  fflush(NULL);
  fp = popen(command, "r");
  if (fp == NULL) {
    printf("Cannot execute command:\n%s\n", command);
    return NULL;
  }

  while(getline(&line, &len, fp) != -1) {
    // +1 below to allow room for null terminator.
    result = (char*) realloc(result, strlen(result) + strlen(line) + 1);
    // +1 below so we copy the final null terminator.
    strncpy(result + strlen(result), line, strlen(line) + 1);
    free(line);
    line = NULL;
  }

  fflush(fp);
  if (pclose(fp) != 0) {
    perror("Cannot close stream.\n");
  }
  return result;
}

int getmem() {
  pid_t pid = getpid();
  char cmd[64];
  snprintf(cmd, 64, "/bin/ps -p %d -o size", pid);
  char* result = exec(cmd);
  if (!result) {
    return 0;
  }
  // Find first newline.
  int pos = 0;
  while (result[pos] != '\n') {
    pos++;
  }
  // Remove the final newline.
  result[strlen(result) - 1] = '\0';
  // Convert to integer.
  int size = atoi(result + pos + 1);
  free(result);
  return size;
}

从技术上讲,我认为 printf(...) 行应该是 fprintf(stderr, ...),但由于某些特定于环境的日志记录原因,我倾向于将 stderr 重定向,这就是我编译和测试代码的方式,所以我正在逐字复制以避免破损。

获取你的PID: pid_t getpid(void); // unistd.h pid_t getpid(void); // unistd.h

解析/proc/<id>/smaps

如果您不关心内存中的共享库,它可能会更简单

ps -p <id> -o %mem进行系统调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM