结论先行
我们知道现代的计算机系统,通常分为虚存和物理内存。其中虚存和物理内存之间存在地址映射。
原则上机器的物理内存是宝贵的,所以,Linux
使用懒惰的策略
来进行分配物理内存,直到虚存被真实使用到!
实验设计
- 用
malloc
来申请内存
仅体现为虚存的增长
- 对
malloc
申请的内存进行逐字节写入访问
造成物理内存被依次分配出来,体现出阶梯状
观察手段
# notice the VIRT RES %MEM cols
top -p $(pidof a.out)
代码
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
void readAnyKey()
{