Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Virtual memory and paging

After this lesson: I can turn a virtual address into a physical address with a page table.

55 min lesson4 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

How every program gets its own memory

Virtual memory gives every process its own private range of addresses. The operating system maps those addresses onto real memory in small blocks called pages. This stops one program reading another program's data, and it lets a program use more memory than the machine has installed.

Behind every address your code uses, a table does the translation.

By the end of this lesson

Note

I can turn a virtual address into a physical address with a page table.

Key words

WordWhat it meansExample
Virtual addressan address a process uses, private to that process
Physical addressthe real place in the memory chips
Pagea fixed-size block of virtual addresses4096 bytes
Framea block of real memory the same size as a page
Page tablethe list that says which frame holds each page
Offsethow far into a page an address is

The cells students fill in are left blank in this preview.

Why a process should not see real addresses

Suppose every program used real addresses. One bug could overwrite another program, or the system itself. Every program would also need to know where free memory is.

Instead, each process sees addresses that start from 0, as if it owned the whole machine. The processor and the system translate each address as it is used.

See how much real memory the lab has

Command
grep -E '^(MemTotal|MemFree)' /proc/meminfo

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Compare virtual size with the part in memory

Command
grep -E '^(VmSize|VmRSS)' /proc/self/status

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Memory is cut into pages and frames

The system cuts virtual addresses into equal blocks called pages, and real memory into blocks of the same size called frames. Any page can sit in any frame.

To translate an address, divide it by the page size. The answer is the page number. The remainder is the offset, the distance into the page. The offset never changes in translation.

Read the real page size in bytes

Command
getconf PAGESIZE

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Split address 8200 into page and offset

Command
python3 -c "print(8200 // 4096, 8200 % 4096)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A page table says where each page is

The page table is a list with one entry for each page. The entry holds the frame number, or a mark that the page is not in memory.

The physical address is the frame number times the page size, plus the offset. The tables below use pages of 16 bytes so the sums stay small.

Worked example: a page table of four pages

PageFrameMeaning
05In memory, in frame 5
12In memory, in frame 2
2noneNot in memory
37In memory, in frame 7

The cells students fill in are left blank in this preview.

Worked example: translating four addresses

Virtual addressPage and offsetFramePhysical address
211 and 522 x 16 + 5 = 37
30 and 355 x 16 + 3 = 83
503 and 277 x 16 + 2 = 114
402 and 8noneNot in memory: a page fault

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think a virtual address is the real position of the data in the memory chips. It is only a name that the page table turns into a real position. Two processes can use address 21 at the same time, and the table sends each to a different frame.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.