Home


Contents

Segmentation

Paging

Memory Allocation

Cache

Garbage Collection


📚 Segmentation

Full blocks of memory used in the physical memory.

<aside> ⚠️ Not used in modern architecture anymore.

</aside>

Pros

Cons

1_FQQaxCq5-0-evraN8inH6A.png


🧩 Paging

Memory is split into pages that are mapped to a frame, or a physical address.

Pages and Frames

A contiguous block memory.

Pages and frames are made of offset and number.

Page offset - Number of bits to represent the page size in virtual memory.

Page number - Number of bits to represent the page address in virtual memory.

Frame offset - Number of bits to represent the frame size in physical memory.

Frame number - Number of bits to represent the frame address in physical memory.

<aside> 💡 The page and frame offset will always be the same in translation.

</aside>

Screenshot 2024-08-20 at 10.25.57 AM.png

Pages do not necessarily need to be contiguous on the physical memory. They have the ability to be stored as an individual page with different neighbouring pages.

paging-in-operating-system-2.png

<aside> ✅ Pages can help solve problems from segmented memory such as:

</aside>

  1. Security: Segmented memory does not use logical memory. If two processes have the same physical address, they can corrupt each others memory.
  2. Not enough RAM: If a segmented memory system runs out of RAM, the process crashes. With virtual memory, the OS will swap the oldest data in the RAM with the page(s) contained in the disk.
  3. Memory Fragmentation: Segmented memory can lead to memory fragmentation issues. For example, if there is 2 GiB of used memory and 2 GiB of free memory, segments can only use the free memory if it is contiguous. Pages have the ability to split and fill these fragments.

Swap Memory

Swap memory is a type of virtual memory that acts as an extension of a computer's physical memory.

When the physical RAM is fully utilized, the OS moves some of the data from RAM to a designated area on the hard drive or SSD, called the swap space. This process frees up space in the RAM for new data.

Swap partition is a reserved space in the disk for swaps. It is hardcoded in the disk.

Swap file holds the same purpose as swap partitions, but can be resized and relocated without modifying the disk partitions.

Page tables

Page tables map page table entries (PTE) to physical addresses.

PTE - An entry for a virtual address to physical address translation.

Multi level page table.

Multi level page table.

<aside> ⚠️ When a translated virtual address does not have a physical address, the memory management unit (MMU) will return DISK and call a page fault. Page faults are very slow, and the OS switches to DMA when it occurs.

</aside>

Direct Memory Access (DMA) is the process of transferring data without the processor’s involvement.

However, pages itself is generally slow due to the expensive steps it takes to translate one page.

  1. Find page translation
  2. Translate the address
  3. Finally access the data in RAM

Translation Lookaside Buffer

TLB is a hardware component that caches virtual to physical address translations, speeding up the process.

tlb-in-os-3-ezgif.com-webp-to-jpg-converter.jpg

TLBs are very small but fast, storing around 4000 entries.

How it works

  1. Page offset is stored in frame offset
  2. Page number is searched in TLB.
    1. If TLB miss, TLB looks at PTE for the nth frame number, and stores it to TLB and physical address.
    2. If TLB hit, TLB stores page number to frame and number
    3. if TLB miss and page number not in PTE, MMU returns DISK and calls a page fault.

Modern architectures usually have two TLBs, one for instructions, and one for data.

Demand Paging

The process of loading pages that are only accessed by a process. Demand paging uses a lazy swapper that brings only necessary pages into the memory.

Lazy swapper diagram.

Lazy swapper diagram.

<aside> 💡 This is the most common type of paging, used in almost every modern architecture.

</aside>


💿 Memory Allocation

memory_layout.png

Stack

The stack is used for managing function calls and local variables.

Local variables, function parameters, and return addresses are stored here.

Automatically managed by the operating system.

Data structure: LIFO

Heap

The heap is used for dynamic memory allocation.

Memory allocation is not automatically managed by the OS, and is larger than the stack. It is the programmer’s responsibility to allocate and deallocate memory.

Uninitialized data (BSS)

This segment holds global and static variables that are declared but not initialized by the programmer.

Example:

static int x;

Without an explicit value, it will be stored in the BSS segment.

Initialized data (Data segment)

This segments holds global and static variables explicitly initialized by the programmer

Example

static int x = 1;

Memory is allocated for these variables at the start of the program and persists until the program terminates.

Text segment

The text segment contains executable code of the program. It is where instructions for the CPU is stored.

Typically read-only to prevent accidental modification of instructions during execution.


💾 Cache


🗑️ Garbage Collection