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

39

对于一个 C++ 程序,是否有可能跟踪程序在某一时刻使用了多少内存?

比如,有一个原型为:

int getEstimatedTotalMemoryUsage();

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

编辑:我正在使用linux,有没有可以为您执行此操作的工具?


1
https://dev59.com/qnVD5IYBdhLWcg3wKoWH - DragonLord
4个回答

38

是的 - 使用POSIX getrusage。从Linux man页面

概要

#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 */
};

1
对于GNU/Linux,这是struct rusage:http://www.gnu.org/s/libc/manual/html_node/Resource-Usage.html - chrisaycock
6
有类似于 Windows 的东西吗? - Lior Kogan
3
并非所有的领域在Linux下都是有意义的。在Linux 2.4中,仅维护ru_utime、ru_stime、ru_minflt和ru_majflt这些字段。自从Linux 2.6以来,也维护ru_nvcsw和ru_nivcsw这两个字段。 - Jay
很好的发现,@Jay - 我在我的Mac上经常使用getrusage(),我甚至没有想到它可能在Linux上没有完全实现。那么,有没有其他信息来源可以提供给OP呢?显然,我应该读整个man页面... - Carl Norum
啊,所以ixrss,idrss等等(我猜)应该是关于在Linux上未实现的内存? - kamziro
啊,发现在Ubuntu上不起作用 :/ 无论如何感谢帮忙,我会再看看的。 - kamziro

5

2

我今天自己需要这个,所以在这里分享测试结果。我相信在任何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重定向,而这样编译和测试代码也能正常工作,所以我直接复制粘贴以避免出错。


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

解析/proc/<id>/smaps

如果您不关心内存总量中的共享库,那么可能更简单

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接