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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Paging →← 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 →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What is the difference between FIFO, LRU and optimal page replacement?
What is Belady's anomaly?
What causes thrashing in virtual memory?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →