250 likes | 276 Views
Memory Management. Memory Manager. Responsible for: Allocating main memory to processes Retrieving and storing the contents to and from main memory when requested Effective sharing of main memory Minimizing memory access time
E N D
Memory Manager • Responsible for: • Allocating main memory to processes • Retrieving and storing the contents to and from main memory when requested • Effective sharing of main memory • Minimizing memory access time • Reference: Nutt, G. (1997), Operating Systems: A Modern Perspective, First Edition, Addison-Wesley, Reading, MA.
Efficient Memory Management • Memory Manager must keep track of: • Memory (location) allocated to a specific process • Best strategies to allocate free memory space to new processes • Three common Strategies are: • Best Fit • Worst Fit • First Fit
3 Memory Allocation Strategies cont Suppose that the memory needs for a process is 12KB: • Best Fit – find the smallest free memory block that will fit the process needs • Idea is minimize wastage of free memory space • E.g., only 1 KB is wasted • Worst Fit – find the largest free memory block that will fit the process needs • Ideal is to increase the possibility that another process can use the left-over space (i.e., 7KB) • First Fit – find the 1st space to fit the memory needs • Minimize the time to analyze the memory space available
Fragmentation • The unused memory spaces left-over from the Best-fit and First-fit are not likely to be useful to fit other processes’ needs.
Allocation / de-allocation of main memory creates a condition called “fragmentation” • Resulted from lots of memory slots (holes) that are possibly not usable • Worst fit tries to reduce the problem of fragmentation. How?
Relocation Applications have to be unaware of their location in memory. When loaded, most OSes will “relocate” memory addresses in the executable code when an app is loaded. Once a process is started it is not possible to relocate the process unless it is aware of the relocation.
Hardware Relocation Logical Address Physical Address From program + Base Address The addition is done by the CPU From the OS
Virtual Memory (VM) • Memory manager uses a portion of memory space from a mass storage device to give an illusion that it is a part of the main memory. • Can run programs whose size is larger than the available main memory.
VM example • Suppose that the main memory has only 32 MB available to run a program of size of 64MB • Memory manager breaks the program into multiple pages of size 4KB and store them in the hard drive. • Memory manager will swap in pages into main memory when needed and swap them out when they are no longer needed. • Consequence, an illusion of 64 KB main memory available.
How VM works • Memory manager keep track of all the pages currently in loaded in main memory in a page table. • Whenever a process requests a new page (not in main memory) a page fault will occur. • The memory manager will handle the page fault by: • Find the new page from the mass storage device • Load it onto the main memory • Update the page table • Re-execute the instruction that caused the page fault (Outdated pages from the main memory will likely be swapped out of the main memory)
Swapping out pages • When the main memory is full (ie., the page table is full) and new pages from VM need to be swapped in • Memory manager needs strategies to replace old pages from main memory with new ones. • These strategies are known as replacement polices.
5 Replacement Polices • Random Replacement • First In First Out • Second Chance • Least Recently used • Least Frequently Used
5 Replacement Polices cont • Random Replacement • Replace pages in main memory randomly • On the average, does not work well • FIFO • Use a queue data structure to keep track of the pages in main memory. • Oldest page at the front (head) and newest page at the back (tail) • Always replace (get rid of) the oldest page • Does not always works, because the oldest page may still be used by the process.
5 Replacement Polices cont • Second Chance • Another version of FIFO to address the problem of FIFO • All pages in the page table are tracked to see if they have been referenced recently by a process • A Reference (R) bit for each page is used for this purpose: • R = 1 when the page is being referenced • R = 0 when the page has not been used after a time period. • The OS will periodically check the (R) bit for each page and move the pages those (R) = 1 to the tail of the queue thus given a 2nd chance.
5 Replacement Polices cont • Least recently Used (LRU) • Replace the page in main memory that has not been used the longest. • Least Frequently Used (LFU) • Counter are used to record the number of times each page have been used • The pages that have been used the least (lowest count) would be replaced.
Hit Ratios • Using each of the five main replacement policies to swap pages between main memory and the page file will result in different hit ratios. • A hit ratio is the number of times that a page is actually found in main memory, as opposed to the number of page faults generated (requiring the memory manager to retrieve the page from virtual memory). • To calculate the hit ratio, we divide the number of non page faults by the total number of page requests (the number of times that data has been sent between the CPU and main memory).
Remember—page faults occur when a page is requested by the CPU that is not currently in the page table (in other words, not currently in RAM). • The memory manager resolves the page fault be swapping an old page from RAM to the page file (on the hard disk), then retrieving the requested page from virtual memory to RAM, then re-executing the instruction. • We use hit ratios to determine which replacement policy will result in the fewest number of page faults, and the fastest overall processing time. • The policy that produces the lowest number of page faults is usually the policy we want to use.
Thrashing: • The main benefit of Virtual Memory is that it artificially increases the overall amount of memory available for the execution of processes. • Swapping pages between RAM and virtual memory slows down the overall processing time. • It takes much more time to locate and retrieve data from a hard disk than it does from RAM. • When your computer spends too much time swapping pages between RAM and the page file, we call this condition thrashing. • Thrashing not only results in longer processing time, it also leads to increased wear and tear on your hard disk.
What Causes Thrashing? • Thrashing is typically caused when you have too many processes running at the same time. • All of these processes will compete for the limited amount of physical memory installed in your computer. • If your computer is thrashing, your applications may stop responding (or run very slowly). At the same time, you may notice your hard drive light blinking (and you may hear the hard drive spinning).
Correcting Thrashing • There are really only two ways to correct thrashing: • Kill (stop) some of the processes (temporary solution) • Install more RAM