Memory Management 1. Memory manager Memory consists of a large array of bytes (words) each with its own address. Main memory is the only storage that CPU can access directly. The CPU fetches instructions from the main memory. Memory Management is very critical for the performance of a computer system. The Memory manager is responsible for allocating and de-allocating memory to the processes that are running on a computer system. 2. Static Memory Allocation Methods 2.1 Single-user Contiguous Each program to be processes is loaded in its entirety into memory. It is given all the memory it requires and the space is contiguous. If the program is bigger than the available memory it is not executed. The program stays in the memory until it is fully executed. The programs are processed sequentially one after the other. No more than one program can be executed at the same time therefore multiprogramming is not supported. This method was used in early computers systems at 1940’s and 1950’s but since it was not efficient it was not used in newer systems. 2.2 Fixed Partitions In this method the main memory is divided into fixed partitions. One partition is used for each program. The size of the partition could only be configured when the machine was shutdown. Once the machine is running then the partitions are fixed. Each job is allocated a partition and it stayed there until it was executed. If a program was smaller than the partition assigned to it then the remainder memory space is wasted. The phenomenon of blocks of unused memory is referred to as fragmentation (internal). A protection scheme is used so a fixed partition used by a program is not allocated to another program. This method introduced multiprogramming but it was more complex than Singleuser Contiguous method. 2.3 Dynamic partitions The programs are given only as much as memory as they request when they are loaded for execution. The memory that they are allocated is still in contiguous blocks. In this way they make more efficient use of the memory since no space is wasted on partitions. However when a job is executed and leaves the memory, the free memory partition may not be the same size as new job and fragments of free memory are created between the memory blocks. The memory manager must keep track of which programs are running on which memory locations and has the responsibility to allocate the free partitions to the incoming programs. There are two popular strategies for allocating the partitions. First fit and Best Fit Best fit: The allocator places a process in the smallest block of unallocated memory in which it will fit. For example, suppose a process requests 12KB of memory and the memory manager currently has a list of unallocated blocks of 6KB, 14KB, 19KB, 11KB, and 13KB blocks. The best-fit strategy will allocate 12KB of the 13KB block to the process. 3 1 First fit: There may be many holes in the memory, so the operating system, to reduce the amount of time it spends analyzing the available spaces, begins at the start of primary memory and allocates memory from the first hole it encounters large enough to satisfy the request. Using the same example as above, first fit will allocate 12KB of the 14KB block to the process. External fragmentation External fragmentation is the development of unusable portions of memory in a dynamic partition memory management scheme. As programs terminate they release their allocated section of main memory which is promptly allocated to other programs ready to execute. If the size of the new program is smaller than that of the dynamic partition, then a hole develops between two allocated memory partitions. This hole is the external fragment. 2.4 Relocatable Dynamic Partitions This method minimize the problem of defragmentation by relocating programs to gather together all the empty blocks and compact them make one block of memory large enough to accommodate some or all the jobs waiting to get in. 3. Compaction Compaction is used to consolidate all the external fragments (free areas in memory) into one contiguous block. In some cases this enhances throughput by allowing more programs to be active at the same time. 4. Relocation Relocation is the process by which programs are repositioned in main memory to allow compaction of free memory areas. When relocation takes place, the addresses specified by a program for either branching or data reference is modified, during execution, to allow the program to execute correctly. Relocation can be time consuming; therefore it should be done sparingly. Some suggestions are: (1) when a certain percentage of main memory is used up, (2) when the number of programs waiting for execution reaches a prescribed upper limit, (3) when a prescribed amount of time has elapsed, (4) combinations of 1, 2 and 3. Quick Quiz 1. Mention 4 advantages of Main Memory Management? Allocation/de-allocation for processes, files, I/O. Maintenance of several processes at a time Keep track of who's using what memory Movement of process memory to/from secondary storage. 2. Explain the term Process? A process is a program in execution: (A program is passive, a process active.) A process has resources (CPU time, files) and attributes that must be managed. 2