测试代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char**argv)
{
char mode = 'm';
size_t size = 1024;
size_t c_size = 0, v_size = 0;
char * p = NULL;
int ret;
while(1) {
ret = scanf("%c %ld", &mode, &size);
if(ret != 2) continue;
printf("action: mode: %c and size: %ld\n", mode, size);
p = malloc(size*1024*1024);
if(p == NULL) {
perror("malloc failed");
continue;
}
v_size += size;
if(mode == 'c') {
p = memset(p, 0, size*1024*1024);
if(p == NULL) {
perror("memset failed\n");
continue;
}
c_size += size;
}
printf("rss: %ld v_size: %ld\n", c_size, v_size);
}
return 0;
}
测试结论
- 单个进程的虚拟地址空间可以超过物理内存和swap的总和。
- malloc 但是未初始化的内存仅占用地址空间,memset 后占用 RSS。
网友评论