[英]Memory usage of current process in C
我需要在C中獲取當前進程的內存使用情況。有人可以在Linux平台上提供如何執行此操作的代碼示例嗎?
我知道cat /proc/<your pid>/status
方法獲取內存,但我不知道如何在C中捕獲它。
順便說一句,這是我正在修改的PHP擴展(授予,我是C新手)。 如果PHP擴展API中有可用的快捷方式,那將更有幫助。
您可以隨時打開/proc
系統中的'files',就像使用常規文件一樣(使用'self'符號鏈接,這樣您就不必查找自己的pid):
FILE* status = fopen( "/proc/self/status", "r" );
當然,您現在必須解析文件以選擇所需的信息。
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
然后手冊定義為:
並非所有字段都已完成; 內核將未維護的字段設置為零。 (提供非維護字段是為了與其他系統兼容,因為它們可能有一天會在Linux上得到支持。)
這是一種非常丑陋且不可移植的內存使用方式,但由於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)手冊頁:
/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)
#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
我發現了這篇文章: 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中測試)
我遲到了,但這可能對其他人在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);
}
上述結構取自4.3BSD Reno。 並非所有字段在Linux下都是有意義的。 在linux 2.4中,僅維護字段ru_utime,ru_stime,ru_minflt和ru_majflt。 從Linux 2.6開始,還保留了ru_nvcsw和ru_nivcsw。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.