mm
mm
mm
Basic Concepts
A computer's memory system is core to its functionality and operations. It refers to the data
storage parts in a computer that can store instructions and data over various time durations.
The primary components of a computer's memory system are the CPU registers, cache, main
memory (RAM), and secondary storage devices.
The CPU contains a small set of registers that provide direct access for storing and operating
on data needed currently. Cache memory is a very high-speed SRAM memory located closer
to the CPU core and used as a buffer for frequently accessed data and instructions. Main
memory or RAM provides generally fast access, allows random access due to which data items
can be read or written in any order. RAM being volatile loses its data when power is switched
off. Secondary storage devices like hard disks, magnetic tapes and SSDs serve as permanent
large capacity storage, maintain data persistently when powered off, but provide relatively
slower access. The backing storage acts as a reservoir supplying data and instructions to main
memory which in turn supplies currently active data to cache and registers as needed by the
CPU for ongoing computations. Memory organization and management thus has significant
influence on performance.
Memory allocation
Contiguous Memory Allocation refers to the technique of allocating physical memory to
processes in a manner where all memory allocated to a process is stored in contiguous blocks.
The contiguous allocation can be done in two ways:
Fixed Partitioning:
In fixed partitioning, the main memory is divided into partitions of fixed sizes that do not
change dynamically. The partition sizes are set by the operating system during OS installation.
When a process arrives, it is allocated memory from a partition large enough to accommodate
it. If no suitable partition is available, the process waits until one frees up.
• Similar to first fit, but the allocator selects the smallest available partition that fits the
process's size.
• Reduces external fragmentation but may lead to small, unusable holes.
Worst Fit:
Compaction:
The process of defragmentation in memory by shuffling allocated and free partitions so all
free memory gets into one contiguous block is called compaction. After compaction, the free
space is available to accommodate any large process requests. However, compaction is a
complex and compute-intensive process. Doing compactions too frequently degrades
performance.
Paging
Paging Principles:
Paging is a memory management technique that allows allocation of non-contiguous memory
to processes. In paging, the logical memory space of a process is broken into blocks of equal
size called pages. The physical main memory is divided into frames of equal size. When a
process is executed, its pages are loaded into available free frames in physical memory.
Paging enables allocating free frames spread across memory to a process unlike contiguous
allocation schemes. It also facilitates swapping to disk when free frames aren’t available,
enabling higher multiprogramming levels and process loads.
Page Allocation:
Initially all frames are marked free. For each process to be executed, its first page is allocated
a free frame, the frame is marked allocated, and entry created in the page table. This
continues until frames exist or process pages get exhausted. If free frames aren't available,
pages are swapped to disk using algorithms like FIFO, LRU etc. When a process terminates, its
frames are reset to available.
Hardware Support:
Paging is facilitated by page tables residing in memory and TLB registers to cache page table
entries, enabling faster lookups. Registers also exist tracking information like page table
length, access bits per page, dirty bits etc. which assist page replacement algorithms and
speedups. The hardware raises page faults on invalid accesses for OS to handle via transfers
from disk.
Locality of Reference:
Programs and code exhibit locality of reference - a tendency that over small time intervals,
limited sets of instructions and data are actively referenced repeatedly before switching to
another set. Virtual memory systems optimize for such behaviour. Recently used active page
working sets stay resident near the CPU minimizing access costs due to locality.
Page Faults:
Attempting to access an address mapping to a non-resident page triggers a page fault -
handled by fetching the required page containing the address from disk into a free frame,
updating mappings and resuming the faulting instruction. Multiple page faults lead to
performance degradation.
Working Set:
The actively referenced pages over recent instruction windows constitute the working set.
Virtual memory subsystems strive to ensure the working set pages are memory resident
through prefetching and replacement of inactive pages.
Optimal Algorithm:
This theoretical algorithm selects the page that will not be used for the longest period in the
future. While providing optimal benchmarks, it is impossible to implement in real-world
scenarios due to the need for future knowledge of page accesses.
Optimality vs Overheads:
Simulation policies provide a spectrum with varying overheads and optimality tradeoffs to
choose from based on constraints and hardware available. Getting page replacements wrong
leads to thrashing severely impacting performance. The policies continue to be an active
research area to accommodate modern application working sets.