Search code examples
operating-systempagingvirtual-memory

Virtual memory location on hard-disk


I was reading about paging and swap-space and I'm a little confused about how much space (and where) on the hard-disk is used to page out / swap-out frames. Let's think of the following scenario :

  1. We have a single process which progressively uses newer pages in virtual memory. Each time for a new page, we allocate a frame in physical memory.
  2. But after a while, frames in the physical memory get exhausted and we choose a victim frame to be removed from RAM.

I have the following doubts :

  1. Does the victim frame get swapped out to the swap space or paged out to some different location (apart from swap-space) on the hard-disk?
  2. From what I've seen, swap space is usually around 1-2x size of RAM, so does this mean a process can use only RAM + swap-space amount of memory in total? Or would it be more than that and limited by the size of virtual memory?

Solution

  • Does the victim frame get swapped out to the swap space or paged out to some different location (apart from swap-space) on the hard-disk?

    It gets swapped to the swap space. Swap space is used for that. A system without swap space cannot use this feature of virtual memory. It still has other features like avoiding external fragmentation and memory protection.

    From what I've seen, swap space is usually around 1-2x size of RAM, so does this mean a process can use only RAM + swap-space amount of memory in total? Or would it be more than that and limited by the size of virtual memory?

    The total memory available to a process will be RAM + swap-space. Imagine a computer with 1GB of RAM + 1GB of swap space and a process which requires 3GB. The process has virtual memory needs above what is available. This will not work because eventually the process will access all this code/data and it will make the program crash. Basically, the process image is bigger than RAM + swap space so eventually the program will get loaded completely from the executable and the computer will simply not have enough space to hold the process. It will crash the process.

    There's really 2 options here. You either store a part of the process in RAM directly or you store it in the swap space. If there's no room in both of these for your process then the kernel doesn't have anywhere else to go. It thus crashes the process.