Linux memory management

LORY
3 min readFeb 7, 2021

Virtual Memory

In linux there is mapping between virtual address and physical address to abstract application’s memory usage .

How it works

Each program maintain a mapping . what it brings :

#1 if not enough physical memory .could map to disk

#2 memory “hole” on physical memory . each program could “jump” to different physical address based on mapping

#3 multiple program trying always being mapped to different physical address

Page table

Linux use page table to store each mapping records .each page table include multiple pages with fixed page size (e.g. 4kb) and page offset to indicate the location on page .

When program trying to access memory will use page number + page offset to query on page table and get virtual address

Similarly Physical address space using page number + offset to find the address to do address translation .But total address space could be different , depends on memory size .

Multi-level page tables

To save space . need multi-level page tables instead of single level .

In single level paging: you need the whole table to access even a small amount of data(less memory references). i.e for 2²⁰ pages each PTE occupying 4bytes as you assumed.

Space required to access any data is 2²⁰ * 4bytes = 4MB

In multilevel paging :only get that specific page to be in the memory while you run the process.

Total size required is now : 4KB + 4KB = 8KB.

Memory Address isolation and sharing

Each program has its own 32-bit virtual address space . use its own page table for address mapping . Os will make sure processes will be mapped to same physical address only when sharing (kernal space)

linux defines different categories for address isolation and sharing :

Kernal : shared

Stack, library, heap, data: isolated

There is random bytes between each category for isolation and security (it is random)

TLB (translation look aside buffer) and L1 cache

To speed up the process . tbl and cache will be used for address translation .

how it works

Summary

Virtual Memory abstract the mapping between physical address and virtual address .so that virtual address could provide more space than physical; isolate application address ;fill physical memory address “hole”

Page table is the storage of “mapping record” . multi-level page table could reduce the storage compared to single level page table . TLB is being used as Lookup “cache” to speed up the address translation process .

--

--

LORY

A channel which focusing on developer growth and self improvement