【发布时间】:2020-07-01 21:16:30
【问题描述】:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int i = 0;
struct rusage r_usage;
while (++i <= 10) {
void *m = malloc(20*1024*1024);
memset(m,0,20*1024*1024);
getrusage(RUSAGE_SELF,&r_usage);
printf("Memory usage = %ld\n",r_usage.ru_maxrss);
sleep (3);
}
printf("\nAllocated memory, sleeping ten seconds after which we will check again...\n\n");
sleep (10);
getrusage(RUSAGE_SELF,&r_usage);
printf("Memory usage = %ld\n",r_usage.ru_maxrss);
return 0;
}
以上代码使用了rusage结构的ru_maxrss属性。它给出了最大驻留集大小的值。这是什么意思?每次执行程序时,它都会给出不同的值。那么请解释一下这段代码的输出?
这些是给出不同输出的相同代码的两次执行的屏幕截图,如何解释这些数字或从这两个输出中可以解释什么?
【问题讨论】:
-
同时使用
man getrusage开始挖掘 -
对于初学者,你知道resident set size 是什么意思吗? “最大值”是指 RSS 在进程生命周期中的最大值。
标签: linux system-calls