Memory Management
Overview Definition Overview of basic hardware issues Fixed memory partitioning Dynamic memory partitioning Placement algorithms Replacement algorithms Paging Segmentation
Introduction Memory is central to the operation of a modern computer system. The CPU fetches instructions from memory (RAM) according to the value of the program counter (PC register). These instructions may cause additional loading from and storing to specific memory addresses. The memory unit sees only a stream of memory addresses; it does not know how they are generated or what they are for (instructions or data).
Basic Hardware Main memory and the registers built into the processor itself are the only storage that the CPU can access directly. Any instructions in execution, and any data being used by the instructions, must be in one of these direct-access storage devices. If the data are not in memory, they must be moved there before the CPU can operate on them. Main memory is accessed via a transaction on the memory bus. Completing a memory access may take many cycles of the CPU clock.
Basic Hardware Contd… In such cases, the processor normally needs to stop, since it does not have the data required to complete the instruction that it is executing. This situation is intolerable because of the frequency of memory accesses. The remedy is to add fast memory between the CPU and main memory. A memory buffer is used to accommodate a speed differential, called a cache.
Basic Hardware Contd… Concerns: the relative speed of accessing physical memory. correct operation to protect the operating system from access by user processes to protect user processes from one another. This protection must be provided by the hardware.
Basic Hardware Contd… We need to ensure that each process has a separate memory space. To do this, we need the ability to determine the range of legal addresses that the process may access and to ensure that the process can access only these legal addresses. We can provide this protection by using two registers, usually a base and a limit. The base register holds the smallest legal physical memory address; the limit register specifies the size of the range.
Types of memory Addresses Logical / Relative Address expressed as a location relative to some known point, e.g., 10th instruction in code Physical or Absolute The absolute address or actual location in main memory 8
Registers Used during Execution Base register Starting address for the process Bounds register Ending location of the process These values are set when the process is loaded or when the process is swapped in 9
For example, if the base register holds 300040 and the limit register is 120900, then the program can legally access all addresses from 300040 through 420939 (inclusive). The CPU hardware compares every address generated in user mode with the base and limit registers. Any attempt by a program executing in user mode to access operating-system memory or other users' memory results in a trap to the operating system, which treats the attempt as a fatal error.
This scheme prevents a user program from (accidentally or deliberately) modifying the code or data structures of either the operating system or other users. Only the operating system can change the value of the registers and prevent user programs from changing the registers' contents.
Logical address space A base and a limit register define a logical address space
Address Binding Usually, a program resides on a disk as a binary executable file. To be executed, the program must be brought into memory and placed within a process. Depending on the memory management in use, the process may be moved between disk and memory during its execution. The processes on the disk that are waiting to be brought into memory (RAM) for execution form the input queue The normal procedure is to select one of the processes in the input queue and to load that process into memory. As the process is executed, it accesses instructions and data from memory. Eventually, the process terminates, and its memory space is declared available.
Logical vs. Physical Address Space An address generated by the CPU is commonly referred to as a logical/virtual address whereas an address seen by the memory unit is commonly referred to as a physical address. The set of all logical addresses generated by a program is a logical address space. The set of all physical addresses corresponding to these logical addresses is a physical address space. The run-time mapping from virtual to physical addresses is done by a hardware device called the Memory Management Unit (MMU).
Mapping with a simple MMU scheme The base register is now called a relocation register. The value in the relocation register is added to every address generated by a user process at the time the address is sent to memory. For example, if the base is at 14000, then an attempt by the user to address location 0 is dynamically relocated to location 14000; an access to location 346 is mapped to location 14346.
Dynamic Loading In our discussion so far, it has been necessary for the entire program and all data of a process to be in physical memory for the process to execute. The size of a process has thus been limited to the size of physical memory. To obtain better memory-space utilization, we can use dynamic loading.
Dynamic Loading Contd… With dynamic loading, a routine is not loaded until it is called. All routines are kept on disk in a relocatable load format. The main program is loaded into memory and is executed. When a routine needs to call another routine, the calling routine first checks to see whether the other routine has been loaded. If it has not, the relocatable linking loader is called to load the desired routine into memory and to update the program's address tables to reflect this change. Then control is passed to the newly loaded routine.
Swapping A process must be in memory to be executed. A process, however, can be temporarily out of memory in a backing store and then brought into memory for continued execution. For example, assume a multiprogramming environment with a round-robin CPU-scheduling algorithm. When a quantum expires, the memory manager will start to swap out the process that just finished and to swap another process into the memory space that has been freed.
Swapping Contd... In the meantime, the CPU scheduler will allocate a time slice to some other process in memory. When each process finishes its quantum, it will be swapped with another process.
Swapping of two processes using a disk as a backing store
Thrashing To many page faults: e.g. swapping out a piece of a process just before that piece is needed The processor spends most of its time swapping pieces rather than executing user instructions 21
Memory Allocation a) Fixed memory Partition Scheme One of the simplest methods for allocating memory is to divide memory into several fixed-sized partitions. Each partition may contain exactly one process. When a partition is free, a process is selected from the input queue and is loaded into the free partition. When the process terminates, the partition becomes available for another process.
Example of Fixed Partitioning of a 64MB Memory 23
Examples of Fixed Partitioning Memory assignment for fixed partitioning 24
Problems with Fixed Partitioning A program may not fit in a partition. Main memory use is inefficient. No program, no matter how small, occupies an entire partition. This is called internal fragmentation. 25
Internal Fragmentation With this approach, the memory allocated to a process may be slightly larger than the requested memory. The difference between these two numbers is internal fragmentation - unused memory that is internal to a partition.
b) Variable memory partition scheme / Dynamic memory partitioning Partitions are of variable length and number Process is allocated exactly as much memory as required Eventually there appears holes in the memory. This is called External fragmentation Must use compaction to shift processes so that they are contiguous and all free memory is in one block 27
Dynamic memory partitioning Contd… In the variable partition scheme, the operating system keeps a table indicating which parts of memory are available and which are occupied. Initially, all memory is available for user processes and is considered as one large block of available memory, a hole. Eventually as you will see, memory contains a set of holes of various sizes.
Variable Memory Partition Scheme Contd… As processes enter the system, they are put into an input queue. Their memory requirements are specified and the amount of available memory space determines which processes are allocated memory. When a process is allocated space, it is loaded into memory, and it can then compete for CPU time. When a process terminates, it releases its memory which the operating system may then fill with another process from the input queue.
Variable Memory Partition Scheme Contd… When a process arrives and needs memory, the system searches the set for a hole that is large enough for this process. If the hole is too large, it is split into two parts. One part is allocated to the arriving process; the other is returned to the set of holes.
Variable Memory Partition Scheme Contd… When a process terminates, it releases its block of memory, which is then placed back in the set of holes. If the new hole is adjacent to other holes, these adjacent holes are merged to form one larger hole. At this point, the system may need to check whether there are processes waiting for memory and whether this newly freed and recombined memory could satisfy the demands of any of these waiting processes.
Example of Dynamic Partitioning 32
Example of Dynamic Partitioning (Contd.... Effects of dynamic partitioning 33
External Fragmentation It is a problem associated with Dynamic memory partitioning External fragmentation exists when there is enough total memory space to satisfy a request but the available spaces are not contiguous; storage is fragmented into a large number of small holes. This fragmentation problem can be severe. In the worst case, we could have a block of free (or wasted) memory between every two processes. If all these small pieces of memory were in one big free block instead, we might be able to run several more processes.
Solutions to External fragmentation Compaction - to place all free memory together in one large block. Permit the logical address space of the processes to be non-contiguous, thus allowing a process to be allocated physical memory wherever such memory is available.
Placement algorithms These are algorithms that determine where in real memory a process piece is to reside Best Fit First Fit Next Fit Worst fit
a) Best-fit algorithm Chooses the block that is closest in size to the request Allocate the smallest hole that is big enough. We must search the entire list, unless the list is ordered by size.. Since smallest block is found for process, the smallest amount of fragmentation is left This strategy produces the smallest leftover hole - Lots of little holes … Memory compaction must be done more often 37
b) First-fit algorithm Scans memory from the beginning and chooses the first available block that is large enough Allocate the first hole that is big enough. Searching can start either at the beginning of the set of holes or at the location where the previous first-fit search ended. We can stop searching as soon as we find a free hole that is large enough. Fastest 38
c) Next-fit Scans memory from the location of the last placement More often allocate a block of memory at the end of memory where the largest block is found The largest block of memory is broken up into smaller blocks Compaction is required to obtain a large block at the end of memory 39
Next Fit Contd... 40
d) Worst fit Allocate the largest hole. Again, we must search the entire list, unless it is sorted by size. This strategy produces the largest leftover hole, which may be more useful than the smaller leftover hole from a best-fit approach.
Paging Paging is a memory management scheme whereby a computer stores and retrieves data from secondary storage for use in main memory. In this scheme, the operating system retrieves data from secondary storage in same-size blocks called pages. Paging is a modern way of implementing virtual memory It uses secondary storage to let programs exceed the size of available physical memory. For simplicity, main memory is called RAM and secondary storage is called "disk“
The basic method *The basic method for implementing paging Breaking physical memory into fixed-sized blocks called frames Breaking logical memory into blocks of the same size called pages. The backing store is divided into fixed-sized blocks that are of the same size as the memory frames. When a process is to be executed, its pages are loaded into any available memory frames from their source (a file system or the backing store).
Paging Contd... Every address generated by the CPU is divided into two parts: a page number and a page offset. The page number - used as an index into a page table. Page offset - combined with base address to define the physical memory address. The page table contains the base address of each page in physical memory. This base address is combined with the page offset to define the physical memory address that is sent to the memory unit.
Paging Contd... When we use a paging scheme, we have no external fragmentation: any free frame can be allocated to a process that needs it. However, we may have some internal fragmentation.
Page Table Operating system maintains a page table for each process Contains the frame location for each page in the process Memory address consist of a page number and offset within the page 46
Fetch Policy Determines when a page should be brought into memory Demand paging only brings pages into main memory when a reference is made to a location on the page Many page faults when process first started Pre-paging brings in more pages than needed More efficient to bring in pages that reside contiguously on the disk 47
Replacement Policy Types of Replacement Policies Determines the page to be replaced in memory Page removed should be the page least likely to be referenced in the near future Most policies predict the future behaviour on the basis of past behaviour Types of Replacement Policies Optimal First In First Out Least Recently Used Clock Policy 48
a) Optimal Policy Selects for replacement that page for which the time to the next reference is the longest Impossible to have perfect knowledge of future events 49
b) First-In, First-Out (FIFO) Treats page frames allocated to a process as a circular buffer Pages are removed in round-robin style Simplest replacement policy to implement Page that has been in memory the longest is replaced These pages may be needed again very soon 50
c) Least Recently Used (LRU) Replaces the page that has not been referenced for the longest time By the principle of locality, this should be the page least likely to be referenced in the near future Each page could be tagged with the time of last reference. This would require a great deal of overhead. 51
Segmentation All segments of all programs do not have to be of the same length There is a maximum segment length Addressing consist of two parts - a segment number and an offset Since segments are not equal, segmentation is similar to dynamic partitioning. 52
The Main Idea – Virtual Memory Definition Not every piece (page) of a process need to be loaded in main memory at the beginning of execution 53
Your task: Find solutions to the problem of internal fragmentation Find out what determines/ what decides the memory management technique to be used Describe the differences and similarities between paging and segmentation
Principle of Locality Program and data references within a process tend to cluster Only a few pieces of a process will be needed over a short period of time Possible to make intelligent guesses about which pieces will be needed in the future This suggests that virtual memory may work efficiently 55