我需要在C中获取当前进程的内存使用情况。有人可以在Linux平台上提供如何执行此操作的代码示例吗?
我知道获取内存使用的cat /proc/<your pid>/status
方法,但我不知道如何在C中捕获它。
答案 0 :(得分:25)
您可以随时打开/proc
系统中的'文件',就像使用普通文件一样(使用'self'符号链接,这样您就不必查找自己的pid):
FILE* status = fopen( "/proc/self/status", "r" );
当然,您现在必须解析文件以选择所需的信息。
答案 1 :(得分:24)
getrusage
库函数返回一个结构,其中包含有关当前进程的大量数据,包括:
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
然而,最新的linux文档说明了这3个字段
(unmaintained) This field is currently unused on Linux
请参阅getrusage(2)
答案 2 :(得分:13)
这是一种非常丑陋且不可移植的内存使用方式,但由于getrusage()的内存跟踪在Linux上基本上没用,所以读取/ proc // statm是我知道获取内存的唯一方法有关Linux的信息。
如果有人知道更清洁,或者更喜欢更多的跨Unix方式来跟踪内存使用情况,我会非常有兴趣学习如何。
typedef struct {
unsigned long size,resident,share,text,lib,data,dt;
} statm_t;
void read_off_memory_status(statm_t& result)
{
unsigned long dummy;
const char* statm_path = "/proc/self/statm";
FILE *f = fopen(statm_path,"r");
if(!f){
perror(statm_path);
abort();
}
if(7 != fscanf(f,"%ld %ld %ld %ld %ld %ld %ld",
&result.size,&result.resident,&result.share,&result.text,&result.lib,&result.data,&result.dt))
{
perror(statm_path);
abort();
}
fclose(f);
}
来自proc(5)man-page:
/proc/[pid]/statm
Provides information about memory usage, measured in pages.
The columns are:
size total program size
(same as VmSize in /proc/[pid]/status)
resident resident set size
(same as VmRSS in /proc/[pid]/status)
share shared pages (from shared mappings)
text text (code)
lib library (unused in Linux 2.6)
data data + stack
dt dirty pages (unused in Linux 2.6)
答案 3 :(得分:7)
#include <sys/resource.h>
#include <errno.h>
errno = 0;
struct rusage* memory = malloc(sizeof(struct rusage));
getrusage(RUSAGE_SELF, memory);
if(errno == EFAULT)
printf("Error: EFAULT\n");
else if(errno == EINVAL)
printf("Error: EINVAL\n");
printf("Usage: %ld\n", memory->ru_ixrss);
printf("Usage: %ld\n", memory->ru_isrss);
printf("Usage: %ld\n", memory->ru_idrss);
printf("Max: %ld\n", memory->ru_maxrss);
我使用了这段代码,但出于某种原因,我总是为所有4个printf()
获得0答案 4 :(得分:6)
我发现了这篇文章:http://appcrawler.com/wordpress/2013/05/13/simple-example-of-tracking-memory-using-getrusage/
简化版:
#include <sys/resource.h>
#include <stdio.h>
int main() {
struct rusage r_usage;
getrusage(RUSAGE_SELF,&r_usage);
// Print the maximum resident set size used (in kilobytes).
printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss);
return 0;
}
(在Linux 3.13中测试)
答案 5 :(得分:4)
我迟到了,但这可能对其他人在linux上寻找常驻和虚拟(以及他们迄今为止的峰值)记忆有帮助。
这可能非常糟糕,但它完成了工作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Measures the current (and peak) resident and virtual memories
* usage of your linux C process, in kB
*/
void getMemory(
int* currRealMem, int* peakRealMem,
int* currVirtMem, int* peakVirtMem) {
// stores each word in status file
char buffer[1024] = "";
// linux file contains this-process info
FILE* file = fopen("/proc/self/status", "r");
// read the entire file
while (fscanf(file, " %1023s", buffer) == 1) {
if (strcmp(buffer, "VmRSS:") == 0) {
fscanf(file, " %d", currRealMem);
}
if (strcmp(buffer, "VmHWM:") == 0) {
fscanf(file, " %d", peakRealMem);
}
if (strcmp(buffer, "VmSize:") == 0) {
fscanf(file, " %d", currVirtMem);
}
if (strcmp(buffer, "VmPeak:") == 0) {
fscanf(file, " %d", peakVirtMem);
}
}
fclose(file);
}
答案 6 :(得分:0)
上述结构取自4.3BSD Reno。并非所有字段都是 - 在Linux下很有意思。在linux 2.4中只有字段ru_utime,ru_stime, ru_minflt和ru_majflt被维护。从Linux 2.6,ru_nvcsw和 ru_nivcsw也得到维护。