Linux Kernel Internals Memory Management

Physical mem vs virtual mem

Virtual memory has many purposes and advantages:

  • Several processes can be executed concurrently.
  • It is possible to run applications whose memory needs are larger than the available physical memory.
  • Processes can execute a program whose code is only partially loaded in memory.
  • Each process is allowed to access a subset of the available physical memory.
  • Processes can share a single memory image of a library or program.
  • Programs can be relocatable — that is, they can be placed anywhere in physical memory.
  • Programmers can write machine-independent code, because they do not need to be concerned about physical memory organization.

The way to achieve that is to have a Virtual Address Space different from the Physical Memory Address. The CPU is equipped to handle Virtual Memory and will translate the Virtual address to its Physical equivalent.(Using an MMU)

Memory on X86_64 CPUs is handled in Pages which are 4KB long, the kernel keeps a Page Table that specify where the CPU should look for a given Page frame.

This makes Memory handling easier as you don't need Physical Memory to be contiguous for Virtual Memory to be.

page

page table

TLB