Virtual Memory, Explained | Introduction to Operating Systems

UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 4.7 Free with a free account

Virtual Memory

Introduction to Operating Systems · Chapter 4: Memory ManagementSection 4.7 · lesson 31 of 839 min · free with a free account

← Address SpacesNext lesson: Paging →

A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

In this lesson

How demand paging, page faults and replacement let programs exceed RAM.

  • Explain how virtual memory and demand paging let programs use more memory than installed RAM
  • Trace the steps the operating system takes to handle a page fault
  • Compare FIFO, LRU and OPT page replacement on a reference string
  • Recognize Belady's anomaly and the causes and cures of thrashing

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,292 words). Read along, or search it.

Opening Scene

More Programs Than 4 GiB Can Hold

Sam, who runs a small online bakery, works on an old laptop with 4 GiB of RAM. On a busy morning, the shop website, a spreadsheet, a photo editor and thirty browser tabs are all open. Together, they ask for far more than 4 GiB. Yet nothing crashes. The laptop is playing a clever trick, called virtual memory. And the trick has a price you can feel, when the fan spins and everything crawls.

Core Idea

Bring Only the Pages You Need

Think of a cookbook with five hundred recipes. Tonight you'll cook two. So the book stays on the shelf, and you bring over just the pages you need. Virtual memory works the same way. Each program gets a large, private range of addresses, as if it owned all the memory it could want. The operating system cuts that range into equal pieces, called pages, and keeps only the pages in use in RAM. The rest wait on the drive, in the backing store: swap space on Linux and macOS, or the page file on Windows.

Demand Paging

Load a Page on First Use

With demand paging, a page comes into RAM only when the program actually uses it, never before. Two wins follow. A program can be bigger than RAM. And more programs fit at once, since each keeps only a few pages in memory.

Page Size

Apple Chips Use 16 KiB Pages

How big is one page? Ask this Mac. The command getconf PAGESIZE prints the page size, in bytes. 16,384 bytes: 16 KiB. Macs with Apple chips use pages of 16 KiB. Most PCs, and most Linux systems, use 4 KiB pages, which is 4,096 bytes.

Code Walkthrough

Counting Page Faults in C

Now watch demand paging happen. When a program touches a page that isn't in RAM, the hardware stops it. That event is a page fault. This C program counts its own page faults. Its faults function asks the system for the running total, with getrusage. main asks malloc for 64 MiB, then reads the fault total. The loop steps through the block one whole page at a time, and touches one byte in each page. Then it reads the total again.

Program Output

One Fault per Page Touched

Build it, and run it. The page size is 16 KiB, as before. And touching the block cost 4,096 page faults. The block holds 4,096 pages: exactly one fault per page, on its first touch. So malloc handed over addresses, not RAM. That's demand paging, measured. These were quick faults. A brand new page has nothing on the drive yet, so the OS just hands it a zeroed piece of RAM.

Step by Step

Ask, Trap, Find, Load, Retry

Here's one page fault, step by step. One, ask: the program uses an address in a page that isn't in RAM. Two, trap: the OS's note for that page says not present, so the CPU jumps into the operating system. Three, find: the OS finds the page on the drive, and picks a free frame, one slot of RAM. If none is free, it evicts a page. Four, load: the drive copies the page into that frame, and the note now says present. Five, retry: the same instruction runs again, and this time it succeeds. The program never knows. So a page fault isn't an error, just a request for help. But when the drive is involved, steps two to four are thousands of times slower.

Performance

Why Faults Must Be Rare

How slow? Take illustrative numbers: 100 nanoseconds for a memory access, and 8 milliseconds to fetch a page from a disk. If one access in ten thousand faults, the average access takes 900 nanoseconds. That's nine times slower. One in a thousand: about 8,100 nanoseconds, or 81 times slower. So faults must be very rare. And that depends on which page the OS throws out when RAM is full.

Quick Check

Faults for a 32 MiB Block

Quick check, with a changed program. It now asks for 32 MiB, and the loop steps 4,096 bytes at a time. With pages of 16 KiB, that touches every page four times. How many faults will the loop cause? Pause, and predict. 2,048. The block is now 2,048 pages, and each page faults only on its first touch. The other three touches in each page find it already in RAM. 8,192 counts touches, not pages. And zero forgets that malloc handed over addresses, not RAM.

Replacement Rules

FIFO, LRU and OPT

When RAM is full and a new page must come in, some page has to leave. A page replacement algorithm picks the victim. This Python program tries three classic rules. FIFO, first in, first out, evicts the page that's been in RAM the longest, like the oldest loaf in the display case. LRU, least recently used, evicts the page left untouched the longest. What you used a moment ago, you'll probably use again. OPT, the optimal rule, evicts the page whose next use is farthest in the future. Nothing can do better, but it needs a crystal ball. Real systems can't see the future, so OPT serves as a yardstick for the others.

Program Output

Faults on Twenty References

The pages a program touches, in order, form a reference string. Here are twenty references, and each rule gets three frames. Run it. F marks a fault, and a dot marks a hit. FIFO faults fifteen times, LRU twelve times, and OPT only nine. Notice the first three references. They fault under every rule: they fill empty frames, and they still count.

Close Look

Which Page Leaves at Step Eight

Look at step eight, where page four arrives. FIFO and LRU both evict page two. It arrived first, and it's gone unused the longest. OPT looks ahead instead. Page two is needed at step nine, page three at step ten, and page zero not until step eleven. So it evicts page zero. One smart choice, and it pays off again and again.

Quick Check

Counting LRU Faults by Hand

Quick check, with a new reference string: one, two, three, one, four, one, two. There are still three frames. How many faults does LRU get? Pause, and decide. Five. The first three references fault, filling the frames. Then page one is a hit. Page four evicts page two, the one unused longest. Page one hits again, and page two comes back with a fault. The program agrees. Here's the new string, ready to run. LRU gets five faults, and OPT gets four. Six is FIFO's count. It evicts page one, the oldest, just before page one is needed again.

Belady's Anomaly

More Frames, More FIFO Faults

You'd expect more frames to mean fewer faults. Try one famous string, first with three frames. FIFO faults nine times. Now give every rule four frames, and run it again. LRU and OPT improve, as expected. But FIFO gets worse: ten faults, with more memory. That's Belady's anomaly. FIFO looks only at a page's age, never at how useful it is. LRU and OPT never get worse with more frames.

Thrashing

Too Many Programs, Too Few Frames

The bigger danger is thrashing. If too many programs share RAM, each one gets too few frames for the pages it needs right now. Every program faults constantly. The drive is busy all the time, and the CPU sits idle, waiting. Adding more programs only makes it worse. The fixes: run fewer programs at once, give each one enough frames for its busy pages, or add more RAM.

Real Case

A Frozen Laptop on a Busy Friday

Back to Sam's laptop. On the busiest Friday, it froze with every tab open, and the drive light stuck on. Ivy, who looks after the computers at a school, didn't blame the shop software. She said: thrashing. She closed the photo editor and a dozen tabs, and the laptop snapped back at once. Later, Sam added more RAM, and the problem stopped coming back.

Key Takeaways

Pages, Faults and Replacement

To recap. Virtual memory gives each program a large, private address space, cut into pages, with only the pages in use kept in RAM. Demand paging brings a page in on its first use, through a page fault. When RAM is full, a replacement rule such as FIFO, LRU or OPT picks the page to evict. And too many programs for too little RAM means thrashing.

About Introduction to Operating Systems at UPI Study

Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Virtual Memory

What is a page fault, and is it an error?
A page fault happens when a program touches a page that is not currently in RAM, and it is a request for help rather than an error. The CPU traps into the operating system, which finds the page on the drive, picks a free frame or evicts a page, loads the page, marks it present, and reruns the same instruction. The program never notices, but faults that need the drive are slow.
What is the difference between FIFO, LRU and optimal page replacement?
FIFO evicts the page that has been in RAM the longest, LRU evicts the page left unused the longest, and OPT evicts the page whose next use is farthest in the future. OPT gives the fewest faults but needs knowledge of future references, so it serves as a yardstick. On one twenty-reference example string with three frames, FIFO faults 15 times, LRU 12 times and OPT 9 times.
What is Belady's anomaly?
Belady's anomaly is the case where giving FIFO page replacement more frames produces more page faults. With the reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, FIFO causes 9 faults with three frames but 10 faults with four. It happens because FIFO looks only at a page's age, not its usefulness. LRU and OPT do not suffer from this anomaly.
What causes thrashing in virtual memory?
Thrashing happens when too many programs share RAM, so each one gets too few frames for the pages it is actively using. Every program faults constantly, the drive stays busy, and the CPU sits idle waiting for pages. Adding more programs makes it worse. The fixes are to run fewer programs at once, give each enough frames for its busy pages, or install more RAM.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Introduction to Operating Systems.

83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Introduction to Operating Systems
3 credits · NCCRS
Enroll →