@rkenmi - Virtual Memory

Virtual Memory


What is virtual memory? What is the difference between physical vs. virtual memory?

Virtual Memory


Back to Top

Updated on September 27, 2020

What is Virtual Memory?

Virtual Memory is best described as a swap file on your hard disk that holds memory information for your running applications. Memory is structured and managed in two different ways; paging and segmentation.

Segmentation

Segmentation allocates memory for each application into physical memory based on their size. These are stored as segments which cannot be broken up into parts. This means that if a user spins up another application when the physical memory is full, that application will not be able to fit into physical memory, so something from memory has to be removed.

To remove it completely would be quite extreme, and the user probably wouldn't want that. Instead, operating systems will put the memory contents of idle applications into the swap file, so that it could free up that space in physical memory. Then, assuming that the new application is smaller in size than the freed up program, the new application will now have its contents in physical memory.

  • Segmentation exposes a problem of memory fragmentation where small, free spaces can start to appear as more and more applications become allocated and freed in memory. These free spaces can't be effectively used because they are usually mapped somewhere between memory contents of two different applications, and application segments have to be stored contiguously.

Paging

Paging allows memory to be defined as small, equally sized blocks (typically 4KB) that can be mapped in such a way that paging blocks do not have to be stored in a contiguous manner in physical memory. To accomplish this, paging tables (on the kernel) are used to map addresses from logical memory to physical memory, using the CPU's MMU (memory management unit).

In paging, every application uses Logical memory. Logical memory is the address space, assigned to a logical partition, that the operating system perceives as its main storage. Therefore, if you have multiple applications running side-by-side, one application's logical memory view would not show memory usage by other applications.

Similar to segmentation, swap files / virtual memory can also be used if more physical memory is needed and idle page blocks happen to be in memory.

  • Since the paging table handles the mapping of small paging blocks, this prevents the memory fragmentation issue that segmentation has. Space is utilized very effectively. Paging blocks won't necessarily be contiguous.
  • Since page blocks for one specific application can be stored in all kinds of places in physical memory (due to paging table mappings), it is slower to access this vs. segmentation, where we can expect the next \(N\) contiguous bytes in memory to belong to that specific application.

Real World Usage

Nowadays most operating systems use a hybrid of both segmentation and paging. Windows and Linux operating systems both use a combination of the two, although they are primarily paging based.

With paging, disk thrashing can occur if page blocks are rapidly being swapped with virtual memory, leading to bad performance.

Overall, there are pros and cons with segmentation and paging individually, but the main issue with segmentation is more problematic than paging

In a nutshell, Virtual Memory allows for more space than there actually exists in actual memory by using swap files. Nowadays it wouldn't be surprising to see swap files as large as 1 GB.


Article Tags:
virtual memorypagingsegmentationrammmucpudisk