/proc/{pid}/status
可以通过读取,/proc/{pid}/status 文件来获取,进程物理内存占用情况
malloc_stats
malloc_stats ptmalloc 中用来打印内存分配情况的一系列函数之一
代码例子
#pragma once
#include <iostream>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
inline int get_memory(const char *prefix)
{
FILE *fd;
char name[32] = {0}, unit[32] = {0}, line_buff[256] = {0}, file[64] = {0};
int i, vmrss = 0;
sprintf(file, "/proc/%d/status", getpid());
fd = fopen(file, "r");
if (fd == NULL)
{
return -1;
}
for (i = 0; i < 40; i++)
{
if (fgets(line_buff, sizeof(line_buff), fd) == NULL)
{
break;
}
if (strstr(line_buff, "VmRSS:") != NULL)
{
sscanf(line_buff, "%s %d %s", name, &vmrss, unit);
printf("%s: %d %s\n", prefix, vmrss, unit);
break;
}
}
fclose(fd);
fflush(stdout);
malloc_stats();
fflush(stderr);
return vmrss;
}
统计信息
通过以上函数,可以获得以下统计数据:
进程占用物理内存大小 | ptmalloc 总分配内存大小 | ptmalloc 正在使用的内存大小 |
---|---|---|
x | x | x |