Skip to main content

Memory Management & Filesystem

Memory Management

Core Concept

  1. Virtual Memory: The foundation of Linux memory management. It creates an abstraction layer, allowing each process to believe it has its own dedicated address space. This address space is virtual—it doesn't directly correspond to physical RAM.
  2. Paging: Virtual memory is implemented using paging. Both virtual and physical memory are divided into fixed-size units called pages (typically 4KB). The CPU uses a mapping structure called a page table to translate virtual addresses used by a process into physical addresses in RAM.
  3. Translation Lookaside Buffer: The TLB is a CPU cache that stores recent virtual-to-physical address translations. It speeds up memory access because the CPU doesn't have to consult the page table every time it needs to access memory.
  4. Page Fault: When a process tries to access a virtual address that isn't currently mapped to a physical page in RAM (e.g., the page is swapped out or hasn't been allocated yet), a page fault occurs. The operating system then loads the required page from disk (swap space or the file system) into RAM.
  5. Swap Space: A dedicated area on disk used to store pages of virtual memory that are not currently in use in RAM. This allows the system to run more processes than it has physical RAM to support.
  6. Anonymous Pages: Memory pages not associated with any file on disk. Used for the heap, stack, and other dynamically allocated memory. These are the pages that get swapped out when memory pressure is high.
  7. File Cache (Page Cache): Used to store recently accessed file data in RAM. Unlike anonymous pages, these are backed by files on disk. When the system needs memory, less-used pages from the file cache are discarded, as they can be easily reloaded from disk.
  8. Slab Allocation: A memory management technique the kernel uses to allocate memory for kernel objects. It allocates memory in caches called slabs, which are tailored to specific object types. This reduces fragmentation and improves performance for kernel memory allocation.

Optimization using Huge Pages:

  1. Huge Pages: These are memory pages that are significantly larger than the standard 4KB page size (e.g., 2MB, 1GB). They reduce overhead from Translation Lookaside Buffer (TLB) misses, improving performance for memory-intensive applications like databases. However, they can lead to memory fragmentation if not used carefully. Allocated using hugetlbfs.

The Role of Huge Pages:

  1. TLB Pressure and Performance Bottleneck: With regular 4KB pages, the TLB can only hold a limited number of mappings. Memory-intensive applications accessing large datasets can quickly exhaust the TLB, causing frequent TLB misses. A TLB miss forces the CPU to walk the page table, significantly slowing down memory access.

  2. Huge Pages to the Rescue: Huge pages address this TLB pressure by increasing the size of individual pages (e.g., 2MB, 1GB). With larger pages, a single TLB entry can map a much larger region of memory. This drastically reduces TLB misses and improves performance for applications working with large datasets.

Filesystem