Back 2 BaseCS : OS : Memory Management

Kshitij Agrawal
Back 2 baseCS
Published in
4 min read12 hours ago

Oops! I caused OOMs ~

Photo by Elijah O'Donnel

As discussed in last article, virtual memory is cool! It abstracts the physical memory and introduces super powers. It allows us to work around the limitations of the physical memory. Essentially, it uses part of the computer’s storage (like a hard drive or SSD) to act as additional RAM.

There is no free lunch. All good things come with some costs. With introduction of virtual memory, we need to manage the movement of data between RAM and physical disk. We need to do it efficiently, as reading things from disk is costly. In this article we discuss some concepts related to this memory management subsystem. Historically, UNIX systems transferred entire processes in and out of physical memory, to something called the ‘swap device’. Such policy is referred to as Swapping. Later on, systems started implementing demand paging — transferring individual pages of virtual memory in and out of physical memory.

Process Swapping

A swap device is a storage section, either a swap partition or a swap file, to which the process data is written to when it is moved out of RAM. Kernel allocates space on swap device in groups of contiguous blocks. To swap out a process, all of its private data including heap needs to be written to the swap device.

While swapping helps manage overloaded memory, there’s a trade-off: moving data between RAM and the hard drive is much slower compared to accessing RAM directly. It might mean that to make a process run again, OS needs to make a lot of disk I/O requests. When the OS is forced to swap data in and out frequently, the system can experience a performance slowdown called thrashing. Thrashing happens when the OS spends more time moving data between RAM and the swap space than executing actual processes!

Thrashing

There is another limitation hidden here. The entire process has to fit on the RAM, and thus is limited by the size of the physical RAM on your system.

Modern systems don’t depend on process swap of entire processes anymore. They rely more on Paging.

Paging

Paging is a key technique that enables virtual memory. It involves dividing both physical memory and virtual memory into fixed-size blocks known as pages (in virtual memory) and frames (in physical memory). Each page is then mapped to a frame in physical memory, allowing non-contiguous memory allocation. As you can feel, this is a fine grained approach to swapping. The pages are much smaller. Also, programs which are larger than main memory can now be executed!

The operating system maintains a page table to keep track of the mapping between virtual pages (used by processes) and physical frames (used by hardware). When a program needs to access a part of memory, the page table is used to translate virtual addresses into physical addresses.

Demand Paging

Paging is typically implemented with a technique called demand paging, where pages are loaded into memory only when they are needed. If a program attempts to access a page that isn’t currently mapped into memory, a page fault occurs, and the operating system might need to load the required page into RAM from disk (this is a major fault). If that mapping request can be satisfied directly from another page in memory or without needing a real read to the disk — it is called a minor fault.

Good and Bad Paging

File system paging, often referred to as ‘good’ paging, is for reads/writes to files in the file system. This type of paging is used for data that exists as part of a file in the filesystem, such as executable files, shared libraries, or any mapped files that a process needs to access. In file system paging, a portion of a file on disk can be mapped directly into a process’s virtual memory space. This is known as memory-mapped file I/O.

Bad paging, otherwise known as Anonymous Paging, involves moving the process’s private data — stacks and heaps (Anonymous — as no filenames exist for this data). Since anonymous pages do not originate from files, when they are swapped out of physical memory, they need to be stored in swap space on disk. When they need to be paged-in, it requires a disk I/O which affects the application performance. That’s why it is ‘bad’!

In coming days and months, we will dig deeper into each of the OS components and learn concepts and implementations. If you are interested to learn these and up-level your computer engineering skills, please consider subscribing to this newsletter.

If you loved this piece, please consider leaving a small tip to keep me motivated! Your support means a lot!

--

--

Kshitij Agrawal
Back 2 baseCS

Director Of Engineering @ Microsoft Azure | IIT Roorkee