sco*_*tts 21 c linux memory memory-management php-extension
我需要在C中获取当前进程的内存使用情况.有人可以在Linux平台上提供如何执行此操作的代码示例吗?
我知道cat /proc/<your pid>/status
获取内存的方法,但我不知道如何在C中捕获它.
顺便说一句,这是我正在修改的PHP扩展(授予,我是C新手).如果PHP扩展API中有可用的快捷方式,那将更有帮助.
caf*_*caf 28
的getrusage
库函数返回包含一大堆有关当前进程,包括这些数据的结构:
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
Run Code Online (Sandbox Code Playgroud)
但是,最新的linux文档说明了这3个字段
(unmaintained) This field is currently unused on Linux
Run Code Online (Sandbox Code Playgroud)
CB *_*ley 27
您可以随时打开系统中的"文件",/proc
就像使用常规文件一样(使用"自我"符号链接,这样您就不必查找自己的pid):
FILE* status = fopen( "/proc/self/status", "r" );
Run Code Online (Sandbox Code Playgroud)
当然,您现在必须解析文件以选择所需的信息.
Jam*_*mes 14
这是一种非常丑陋且不可移植的内存使用方式,但由于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);
}
Run Code Online (Sandbox Code Playgroud)
从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)
Run Code Online (Sandbox Code Playgroud)
小智 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);
Run Code Online (Sandbox Code Playgroud)
我使用了这段代码,但出于某种原因,我总是为所有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;
}
Run Code Online (Sandbox Code Playgroud)
(在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);
}
Run Code Online (Sandbox Code Playgroud)