Hands-On System Programming with Go
上QQ阅读APP看书,第一时间看更新

Virtual memory

Unix uses the paged memory management technique, abstracting its memory for each application into contiguous virtual memory. It also uses a technique called swapping, which extends the virtual memory to the secondary memory (hard drive or solid state drives (SSD)) using a swap file. 

When memory is scarce, the operating system puts pages from processes that are sleeping in the swap partition in order to make space for active processes that are requesting more memory, executing an operation called swap-outWhen a page that is in the swap file is needed by a process in execution it gets loaded back into the main memory for executing it. This is called swap-in.

The main issue of swapping is the performance drop when interacting with secondary memory, but it is very useful for extending multitasking capabilities and for dealing with applications that are bigger than the physical memory, by loading just the pieces that are actually needed at a given time. Creating memory-efficient applications is a way of increasing performance by avoiding or reducing swapping.

The top command shows details about available memory, swap, and memory consumption for each process:

  • RES is the physical primary memory used by the process.
  • VIRT is the total memory used by the process, including the swapped memory, so it's equal to or bigger than RES.
  • SHR is the part of VIRT that is actually shareable, such as loaded libraries.