Memory Virtualization: Memory Management Flashcards
Why do we virtualize memory?
Virtual memory serves two purposes: - First, it allows us to extend the use of physical memory by using disk. - Second, it allows us to have memory protection, because each virtual address is translated to a physical address.
What does virtual address space contain?
Program Code (Text Segment): Contains the program’s executable instructions. It’s read-only to prevent accidental changes.
Heap: Used for dynamically allocated memory during runtime, like when using malloc() in C. It expands as more memory is needed. Stack: Stores local variables and manages function calls. Operates in a last-in, first-out manner and shrinks or grows in the opposite direction of the heap.
Why do we use virtual address space?
To be able to locate where in physical memory the data is stored. It is translated from Virtual Address to Physical Address
What component does the translation of virtual to physical address?
MMU (memory-managment unit)
What is the goal of virtualization?
Transparency: user programs should not be aware of the messy details-
Efficiency: minimize overhead and wastage in term of memory space and access time-
Isolation and protection: A user process should not be able to access anything outside its address space
Does the OS have its own address space? If not, where is it?
OS is not separate process with its own address space- Instead, OS code is part of the address space of every process- A process sees OS as part of its code (library)- Page table map the OS addresses to OS code
What does the stack contain? What does the heap contain?
The Stack:
Contains local variables, function parameters, return addresses, and control data. Automatically adjusts in size as functions are called and completed.
The Heap:
Used for memory that is allocated and freed during program run time, like with malloc in C. Stores global data and large data structures. Its size is managed manually by the programmer
What does malloc() do? What does free() do?
malloc(): Allocates a specified amount of memory during a program’s run time.
free(): Releases previously allocated memory back to the system.
What are the procedures during address translation?
Determine Address Space and Page Size:
Address space: 64 bytes.
Page size: 16 bytes.
Total pages: 64 bytes / 16 bytes/page = 4 pages.
Split Virtual Address into VPN and Offset (Example: 21): Convert to binary: 21 is "01 0101". Virtual Page Number (VPN): First 2 bits (01). Offset: Last 4 bits (0101). Translate VPN to Physical Frame Number (PFN): Use page table. Example: VPN 1 maps to PFN 7. Combine PFN and Offset for Physical Address: PFN 7 in binary is 111. Combine with offset: "111 0101". Convert to decimal: Physical address is 117.
What is the role of OS in the translation?
Maintains Free List of Memory: Tracking unallocated memory.
Allocates Space During Process Creation; Cleans Up When Done: Allocating and deallocating memory for processes. Maintains Information in PCB: Storing information about allocated space in the Process Control Block. Sets Address Translation Information in Hardware: This is similar to managing page tables and TLB. Updates Information Upon Context Switch: Adjusting memory management structures when switching between processes. Handles Traps Due to Illegal Memory Access: Part of memory protection and similar to handling page faults.
What is the role of hardware in address translation?
The CPU provides privileged mode of execution, this makes it so the instructions set has priviliged instructions to set translation information (base and bounds).
Then the Hardware(MMU) uses this information to perform translation on every memory access (instruction fetch, load or store)If the translation is illegal, the MMU generates traps to OS(eg, VA is out of bounds)
What is the formula for physical address?
PA = base register + VA
What are the problems with base and bounds?
If stack and heap are small, space between them is wasted. This is called internal fragmentation.This is caused by fixed size slots (due to our assumptions as same size address space). - Requires a big chunk of free space in the middle
How does the virtual address know which segment contains what (segmentation storing)?
Using the two first bits”00” = Code”01” = Heap”10” = Stack”11” = Unused
How can processes share memory (segmentation)?
Protection in segmentation includes extra bits per segment for read, write, and execute permissions. Processes perceive their memory as private, while the OS may share it secretly. The hardware checks if access is permissible.
Why do we use segmentation?
Unused space between stack and heap need not be allocated in physical memory- Allowing for more address spaces in physical memory we can use segmentation
What are the problems with free space management within segmentation?
Physical memory becomes full of holes of free space (external fragmentation)
What is external fragmentation?
Physical memory becomes full of holes of free space (external fragmentation)
What is a solution to external fragmentation within segmentation?
One solution to manage memory is to compact physical memory by rearranging segments. This involves stopping processes, moving their segments, and updating segment registers to new locations, which is memory-intensive and time-consuming.
Is there a better approach to avoid external fragmentation with segmentation?
Use a free list management algorithm, like the best fit algorithm, which maintains a list of free memory spaces and allocates the chunk closest in size to the requested amount.
What are the benefits of segmentation?
Support sparse address spaces (address spaces with a lot of empty spaces)- Avoid wasting memory between logical segments of an address space- Fast translation- Code sharing- No internal fragmentation
What are the problems with segmentation?
External fragmentation occurs in segmentation, leaving odd-sized free memory pieces.
This fragmentation issue doesn’t happen with paging.
Memory allocation becomes more difficult due to fragmentation.
It’s fundamentally challenging to avoid external fragmentation in segmentation.
Segmentation lacks flexibility for sparse address spaces.
If the heap is sparsely used, the entire heap must be retained in memory.
How to manage free space, when satisfying variable-sized requests?
Splitting: Divide larger memory segments to fit the requested size.
Coalescing: Merge adjacent free chunks into larger blocks.
Fragmentation Management: Combine smaller free spaces to fulfill larger requests.
Size Tracking: Monitor the sizes of allocated and free memory regions.
Free List: Use a list to efficiently track and allocate free memory segments.
What is splitting? What is coalescing?
Example, trying to allocate 1 bytes when we have two 10 bytes segment available:- Find a free chunk that satisfy the request and split it into two- Return the first chunk to the caller (1 byte)Second chunk remain on the list (9 bytes)
It allows us to combine free segment next to each other in memory
Why would we need coalescing?
It occur when we for example have three free segment next to each other with size 5. If we try to allocate 7 bytes it will fail. That’s why we coalesce them to get 15 byte available in one segment
In free space management, how do we track the size of allocated regions?
In free space management, the size of allocated regions is tracked by creating a header field in each allocation containing size information and sometimes pointers for quicker freeing.