Swapping
- A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution
- Total physical memory space of processes can exceed physical memory
- Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images
- Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed
- Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped
- System maintains a ready queue of ready-to-run processes which have memory images on disk
进程可暂时从内存交换到后备存储区,然后再返回内存继续执行
进程的总物理内存空间可能超过物理内存
后备存储 - 足够大的快速磁盘,可容纳所有用户的所有内存映像副本;必须提供对这些内存映像的直接访问权限
滚出、滚入 - 基于优先级的调度算法使用的交换变体;低优先级进程被交换出去,以便加载和执行较高优先级的进程
交换时间的主要部分是传输时间;总传输时间与交换的内存量成正比
系统为磁盘上有内存映像的待运行进程维护一个就绪队列
- Does the swapped out process need to swap back in to same physical addresses?
- Depends on address binding method
- Plus consider pending I/O to / from process memory space
- Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows)
- Swapping normally disabled
- Started if more than threshold amount of memory allocated
- Disabled again once memory demand reduced below threshold
被换出的进程是否需要换回相同的物理地址?
取决于地址绑定方法
还要考虑进程内存空间的待处理 I/O
许多系统(如 UNIX、Linux 和 Windows)都有修改过的交换版本
通常禁用交换
当分配的内存超过阈值时启动
一旦内存需求降至阈值以下,则再次禁用
Schematic View of Swapping
Context Switch Time including Swapping
- If next processes to be put on CPU is not in memory, need to swap out a process and swap in target process
- Context switch time can then be very high
- 100MB process swapping to hard disk with transfer rate of 50MB/sec
- Swap out time of 2000 ms
- Plus swap in of same sized process
- Total context switch swapping component time of 4000ms (4 seconds)
- Can reduce if reduce size of memory swapped – by knowing how much memory really being used
- System calls to inform OS of memory use via request_memory() and release_memory()
上下文切换时间(包括交换
如果CPU上的下一个进程不在内存中,则需要交换出一个进程并交换入目标进程
那么上下文切换时间可能会非常长
将 100MB 进程交换到硬盘,传输速率为 50MB/秒
交换出时间为 2000 毫秒
加上换入相同大小的进程
上下文切换交换组件的总时间为 4000 毫秒(4 秒)
通过了解实际使用的内存量,可减少内存交换的大小
通过 request_memory() 和 release_memory() 系统调用通知操作系统内存使用情况
- Other constraints as well on swapping
- Pending I/O – can’t swap out as I/O would occur to wrong process
- Or always transfer I/O to kernel space, then to I/O device
- Known as double buffering, adds overhead
- Standard swapping not used in modern operating systems
- But modified version common
- Swap only when free memory extremely low
- But modified version common
关于交换的其他限制
待处理的 I/O--不能交换,因为 I/O 会发生在错误的进程中
或者总是将 I/O 传输到内核空间,然后再传输到 I/O 设备
称为双重缓冲,会增加开销
现代操作系统不使用标准交换
但修改版很常见
只有当可用内存极少时才进行交换
Swapping on Mobile Systems
通常不支持
基于闪存 空间小 写入次数有限
移动平台上闪存和 CPU 之间的吞吐量较低
如果内存不足,可使用其他方法释放内存
iOS 要求应用程序主动放弃分配的内存
只读数据会被抛出,并在需要时从闪存中重新加载
未释放内存会导致应用程序被终止
如果可用内存不足,Android 会终止应用程序,但会先将应用程序状态写入闪存,以便快速重启
如下所述,两种操作系统都支持分页
Swapping with Paging