Download presentation
Presentation is loading. Please wait.
Published bySheena Lane Modified over 9 years ago
1
1 Memory Management Requirements of memory management system to provide the memory space to enable several processes to execute concurrently to provide a satisfactory level of performance for users to protect processes from each other to enable sharing of memory space between processes to make addressing transparent
2
2 Basics Source code into the object code via compilation or assembly. Software systems of any size are usually made up of several modules which are joined together into one complex system using a linker. At the time the source code is translated into an object module, it contains external references. The compiler will generate a symbol table which is used to resolve these references during linking. The resulting module is known as the load module. At this point all addresses generated are logical addresses as the compiler and linker have no idea where the code will be placed in memory. These must be converted into physical addresses before they can be loaded and run. If these external references are resolved before the execution of the program, the linking is referred to as static, whereas if they are made at execution time they are known as dynamic. Finally load module from secondary storage into main memory. The logical to physical address translation is known as binding. It can occur at programming time, compile time or link time in an embedded system or simple computer system. All three strategies are static binding techniques and the resultant code is not relocatable. When binding occurs at runtime it is known as dynamic binding all memory references are logical but must be translated into physical addresses before being presented to the memory. The logical to physical transformation is accomplished using an address map.
3
3 Relocatable Code If virtual address space is organised as below, then the basic requirement for modules is to be held contiguously in physical memory and to contain addresses relative to their first location. The physical address of this first location is called the base of the process. Physical memory is much smaller than Virtual memory. The user program is shown starting at an arbitrary address and sharing memory with other programs Virtual Memory Physical Memory The simplest form of dynamic relocation hardware is a single base register and a memory management unit (MMU) to perform the translation
4
4 UNUSED USER PROCESSES OPERATING SYSTEM Single Process Allocation Memory Management Fixed Partition Allocation PROCESS A PROCESS C PROCESS B OPERATING SYSTEM Process C = 300k Process B = 200k Process A = 150k Partition3 400k Partition 2 300k Partition 1 200k
5
5 Fixed Partitions Fixed size partitions to allow for large and small processes Wasted space leads to internal fragmentation Where internal infers space allocated for a process System must manage memory protection Disadvantages process can’t run because no partition of suitable size available internal fragmentation wastes space
6
6 Variable Partitions AVAILABLE SPACE PROCESS C PROCESS B PROCESS A OPERATING SYSTEM 1000k 750k 450k 250k 100k 0k PROCESS C PROCESS B PROCESS A OPERATING SYSTEM 1000k 750k 450k 250k 100k 0k New PROCESS D 225k 975k Initially processes are loaded until memory full or no spaces large enough
7
7 FREE PROCESS C FREE PROCESS A OPERATING SYSTEM 1000k 750k 450k 250k 100k 0k PROCESS C terminates leaving three holes to coalesce PROCESS A OPERATING SYSTEM 1000k 750k 450k 250k 100k 0k Variable Partitions (a)(b) In (a) not enough space for new process although total space o.k Wasted space is called external fragmentation where: external = space without allocation
8
8 Storage placement policies Memory allocation is the process of assigning blocks of memory on request – performed by the allocator receives memory from the operating system in a small number of large blocks that it must divide up to satisfy the requests for smaller blocks. It must also make any returned blocks available for reuse. –Best fit policy –First fit policy –Worst fit policy –Buddy System Best fit is intuitively appealing, since it would appear that minimum space is wasted and is simple. However most programs grow and shrink dynamically. There are two reasons for this: The stack – it’s size increases with the depth of function calls The use of dynamically acquired memory using the malloc command (in ‘C’), which permits it to acquire extra memory in a structure called the heap.
9
9 Storage placement policies Worst fit doesn’t look too promising. However, worst fit could possibly leave a hole large enough to accommodate another process, while best fit, in trying to minimise the unused space could create a hole too small to be useful. First fit, appears to be too indiscriminate but there is very little overhead in implementing it. In the first fit algorithm, the allocator keeps a list of free blocks (known as the free list) and, on receiving a request for memory, scans along the list for the first block that is large enough to satisfy the request. The first fit algorithm performs reasonably well, as it ensures that allocations are quick.
10
10 NEW PROCESS 150K 100k 300k 200k 400k First fit, Hole residue = 150k Best fit, Hole residue = 50k Worst fit, Hole residue = 250k AVAILABLE HOLE SIZES (in memory order) ALLOCATION SELECTION Memory Management Best fit – hole into which process fits most tightly First fit – first available hole Worst fit – leaves maximum amount of free space
11
11 Buddy System In a buddy system, the allocator will only allocate blocks of certain sizes which are usually either powers of two, or form a Fibonacci sequence. Blocks can be divided into two smaller blocks. When the allocator receives a request for memory, it rounds the requested size up to a permitted size. If the free list for that size is empty, the allocator splits a block from a larger size. When blocks are recycled, there may be some attempt to merge adjacent blocks into ones of a larger permitted size (coalescence). A binary buddy heap before allocation A binary buddy heap after allocating a 8 kB block A binary buddy heap after allocating a 10 kB block; note the 6 kB wasted because of rounding up
12
12 Memory Management FREE PROCESS C PROCESS A OPERATING SYSTEM Need to suspend all process while compaction takes place Major overhead – particularly in time critical systems When to perform compaction when process terminates when process cannot load due to fragmentation at fixed intervals when user decides to Compaction of free space
13
13 Managing Memory If the memory requirements of the system exceed the available main memory there are at least three ways of solving the problem: –Overlays –Swapping –Paging Overlays are sections of an application's code or data that are stored on secondary memory when not in use. Swapping - if memory is needed, a ready process is selected to be swapped out. Swapping a process out of main memory involves copying the data segments of that process to a disk. When a process is selected to be run, if that process is swapped out, the system must first find or make space in main memory, then swap that process in, before running it.
14
14 ED CB A Overlays – function call hierarchy k2 k1 ACEACE ACDACD ABAB Overlays – address assignments Since B and D do not call each other, they need not reside in memory at the same time. The same is true of functions D and E; furthermore their presence is needed only when C is resident.
15
15 Simple Paging PAGE 1 2 3 4 5 6 PAGE 1 2 3 4 PAGE 1 2 3 4 5 Process A Process B Process C A1 A2 A3 A4 A5 A6 B1 B2 B3 B4 B5 C1 C2 C3 C4 - Memory space Paging System Allocation
16
16 Simple Paging A1 A2 A3 A4 A5 A6 - C1 C2 C3 C4 - A1 A2 A3 A4 A5 A6 D1 D2 D3 E1 E2 C1 C2 C3 C4 E3 E4 - New process D New process E A1 A2 A3 A4 A5 A6 F1 F2 F3 H1 H2 G1 G2 H3 H4 - F4 (a)(b) (c) Updated Paging System
17
17 Simple Paging 01010101100110111110010110011011 0 0111 11001 20001 31011 40000 51110 60110 Memory address Converted memory address p pd Page Address Translation
18
18 Page Tables Address Translation
19
19 Memory Leakage A memory leak is a particular kind of unintentional memory consumption by a computer program where the program fails to release memory when no longer needed. The program in this case is part of some very simple software designed to control a lift This part of the program is run whenever anyone inside the lift presses the button for a floor. When a button is pressed: Get some memory, which will be used to remember the floor number Put the floor number into the memory Are we already on the target floor? { If so, we have nothing to do: finished } Otherwise { Wait until the lift is idle Go to the required floor Release the memory we used to remember the floor number }
20
20 #include void f(void) { void* s; s = malloc(50); /* get memory */ return; /* memory leak */ main() { while (1) f(); /* This function call will fail to malloc sooner or later */ } Memory was available and pointed to by s, but not saved. After this function returns, the pointer is destroyed and the allocated memory becomes unreachable. To "fix" this code, either the f() function itself needs to add "free(s)" somewhere.
21
21 Coffee Break
22
22 Correspond to logical divisions in a process i.e. code segments/ modules FREE A4 A3 B2 A2 A1 B3 B1 OPERATING SYSTEM SEGMENT 4 SEGMENT 3 SEGMENT 2 SEGMENT 1 SEGMENT 3 SEGMENT 2 SEGMENT 1 Process Structures Memory allocation Process A Process B Segmentation
23
23 Memory Management 00110001011110100101001001111011 Logical addressPhysical address + LENGTHBASE ADDRESS 0 1 2 3 010110110111 010100010000001 4 s displacement d 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 1 1 0 1 0 0 1 0 1 0 0 1 0 0 1 1 1 1 0 1 1
24
24
25
25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.