Memory Management in short
- Abhilasha
- Jul 14, 2024
- 5 min read
Address Space
CPU Access: To run instructions and access data in main memory, the CPU needs unique addresses for that data.
Definition: Address space is the range of valid addresses used to identify data within a memory allocation.
Structure: This range starts at byte 0 and ends at the last byte's offset in the memory allocation.
Linear Address Space: The continuous range of addresses available to a running program.
Linear and Physical Addresses
Linear/Virtal Addresses: These terms are interchangeable, referring to the addresses used by programs.
Physical Address Space: These are the actual addresses used by the CPU to access physical memory, derived by translating linear addresses.
IA-32 Architecture
Registers: Fast, small memory locations in the CPU used for temporary storage. IA-32 CPUs have eight 32-bit general-purpose registers and others for controlling CPU behavior.
Segmentation and Paging:
Segmentation: Divides the 32-bit linear address space into variable-length segments. Often disabled in 64-bit mode.
Paging: Virtualizes the linear address space by breaking it into fixed-size pages, which can be mapped to physical memory arbitrarily.
Intel 64 Architecture
Similar to IA-32: Shares many features with IA-32 but with differences:
Expanded Registers: Registers hold 64 bits.
64-bit Linear Addresses: Supports a much larger address space, up to 2^64 bytes.
Memory Management
Overview: Refers to the OS's methods for allocating, deallocating, and organizing physical memory.
Components:
Virtual Memory: Provides each process with its own private address space, separating logical and physical memory.
Demand Paging: Loads only necessary pages into memory, with others moved to secondary storage (page file/swap).
Shared Memory: Allows processes to share memory regions for communication and efficiency.
Stacks and Heaps:
Stacks: Store temporary function data.
Heaps: Store dynamically allocated data.
File System: Supports memory-mapped files for efficient access and modification.
I/O Device Drivers: Manage data transfer to and from hardware devices, abstracting the device details.
Virtual Memory
Private Virtual Address Space: Each process is provided with its own virtual address space by the operating system.
Logical vs Physical Memory: This abstraction separates what the process perceives as memory (logical) from the actual physical memory in the machine.
Memory Management Responsibility: The memory manager handles the movement of memory regions between physical memory and secondary storage (like disk) to optimize physical memory usage.
Translation Process: During execution, the Memory Management Unit (MMU) collaborates with the memory manager to convert virtual addresses used by the process into actual physical memory addresses.
Handling Virtual Memory
Access to Secondary Storage: When a thread tries to access a virtual address that is currently stored in secondary storage (due to being swapped out), the operating system brings that data back into physical memory. This process is often triggered by a page fault.
Address Partitioning: Operating systems typically divide the accessible addresses into those reserved for system-level operations (operating system space) and those that are specific to each process (private address space).
Consistency Across Processes: System-level address ranges remain consistent across all processes, while private address ranges vary depending on the executing process's needs and size.
Demand Paging
Definition: Demand paging is a strategy in virtual memory management where pages of memory are not immediately loaded into main memory (RAM) when a program starts, but are instead loaded only when they are needed.
Optimizing Memory Usage: This approach aims to optimize the usage of physical memory by loading only the pages of memory that are required by the running processes, rather than loading entire processes into memory.
Secondary Storage Usage: When a page that is needed is not currently in main memory (it's not resident), the operating system triggers a page fault.
Handling Page Faults:
Page Fault Generation: If a thread attempts to access a page that is not in RAM, the hardware raises a page fault exception.
Operating System Response: Upon a page fault, the operating system uses information stored in paging structures to decide how to handle it.
Page Retrieval: It may retrieve the needed page from secondary storage (like a page file or swap partition on disk) and load it into physical memory.
Memory Management Unit (MMU): The MMU plays a critical role in translating virtual addresses to physical addresses and managing page faults.
Example Scenarios:
If a page of memory contains data that has been swapped out to a page file due to low physical memory, demand paging allows the operating system to bring that page back into RAM when it's needed again.
Pages of memory that are rarely accessed may be kept in secondary storage until they are accessed, optimizing the use of limited physical memory resources.
Shared Memory
Definition: Shared memory refers to a mechanism in operating systems that allows multiple processes to access and manipulate the same memory region using their respective virtual address spaces.
Isolation and Access: While processes typically have isolated address spaces for security and stability, shared memory provides a controlled way for processes to communicate and share data efficiently.
Mechanism:
Mapping: A shared memory region is mapped into the virtual address spaces of multiple processes.
Access: Processes can then read from and write to this shared region as if it were their own private memory.
Communication Efficiency:
Inter-Process Communication (IPC): Shared memory is commonly used to facilitate fast communication between processes. Instead of using slower IPC methods like message passing or file I/O, processes can directly exchange data by accessing shared memory.
Data Sharing: It allows processes to share large datasets or structures without duplicating them in each process's memory space, thus conserving physical memory resources.
Examples:
Dynamic Libraries: Shared memory is used extensively in dynamic linking where multiple processes can share the same instance of a library loaded into memory, reducing memory usage.
Communication Buffers: Shared memory is used in applications where real-time data exchange is critical, such as multimedia applications or network servers.
Management:
Concurrency: Shared memory requires careful synchronization mechanisms (like semaphores or mutexes) to prevent data corruption when multiple processes access shared data simultaneously.
Lifecycle: The operating system manages the lifecycle of shared memory segments, ensuring they are properly cleaned up when no longer needed.
Stacks
Definition: In the user address space, the stack is a region designated for managing temporary data during function execution.
Structure: Data is organized into stack frames, each containing function parameters, local variables, and return addresses.
Operation: As functions are called, stack frames are pushed onto the stack; when functions return, frames are popped off.
Mode Usage: Processes in both kernel and user modes utilize separate stacks to manage function execution efficiently.
Heaps
Definition: The heap is where applications dynamically allocate and manage data that persists beyond the scope of individual functions.
Characteristics: Unlike the stack, heap data can exist for the entire process lifespan and its size or content may not be known at compile time.
Usage: Applications allocate memory on the heap as needed and release it when no longer required, allowing for flexible memory management.
Examples: Data from files, network transfers, and user input are commonly stored in the heap, providing long-term storage and accessibility.
File System
Memory Mapping: Modern operating systems support memory-mapped files, allowing files or parts of files to be accessed directly in the virtual address space.
Access and Modification: Once files are mapped into memory, they can be accessed and modified like regular in-memory data structures such as arrays.
Page Management: File data is read into memory in pages as addresses within those pages are accessed, facilitating efficient data handling.
Shared Access: Regions of file data mapped into memory can be shared among processes, enabling collaboration and efficient resource utilization.
I/O Device Drivers
Abstraction: Device drivers abstract the complexities of device operation and data transfer, shielding applications from low-level details.
Communication: Drivers interact with device controllers through registers, managing the device's functionality and data flow.
Address Spaces: CPU architectures handle I/O differently:
Some architectures allocate a separate address space for I/O devices, necessitating privileged instructions for access.
Other architectures map I/O device memory and registers directly into the virtual address space, simplifying access and integration with application code.
Comments