Memory management keeps track of each and every memory location, regardless of either it is allocated to some process or it is free. It
- Checks how much memory is to allocated to process.
- Decided which process will get memory at what time.
- Tracks whenever some memory gets freed or unallocated and correspondingly it updates the status.
Process Address Space
The operating system takes care of mapping the logical addresses to physical addresses at the time of memory allocation to the program. There are three types of address used in a program before and after memory is allocated.
- Symbolic address: The address used in a source code.
- Relative address: At the time of compilation, a compiler converts symbolic address into relative address.
- Phsical address: The loader generates these address at the time when a program is loaded into main memory.
The runtime mapping from virtual to physical address is done by the memory management unit(MMU) which is a hardware device. The value in the base register is added to every address generated by a user process.
Static vs Dynamtic Loading
Static Loading: At the time of compilation, the complete programs will be compiled and linked without leaving any external program or module dependency. At the time of loading, the absolute program(and data) is loaded into memory in order for execution to start.
Dynamtic Loading: The compiler will compile the program and for all the modules which you want to include dynamically, only references will be provided and rest of the work will be done at the time of execution. Dynamic routines of the library are stored on a disk in relocatable form and are loaded into memory only when they are needed by the program.
Static vs Dynamic Linking
Static Linking: The linker combines all other modules needed by a program into a single executable program to avoid any runtime dependency.
Dynamic Linking: It is not required to link the actual module or library with the program, rather a reference to the dynamic module is provided at the time of compilation and linking. Dynamic Link Libraries(DLL) in Windows and Shared Objects in Unix are good examples of dynamic libraries.
Swapping is a mechanism in which a process can be swapped temporarily out of main memory (or move) to secondary storage(disk) and make that memory available to other processes. At some later time, the system swaps back the process from the secondary storage to main memory.
Swapping is also known as a technique for memory compaction.
Main memory usually has two partitions
- Low Memory - Operating system resides in this memory.
- High Memory - User processes are held in high memory.
Operating system uses the following memory allocation mechanism.
In this type of allocation, relocation-register scheme is used to protect user processes from each other, and from changing operating-system code and data. Relocation register contains value of smallest physical address whereas limit register contains range of logical addresses. Each logical address must be less than the limit register.
In this type of allocation, main memory is divided into a number of fixed-sized partitions where each partition should contain only 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.
There are two types of fragmentation:
- External fragmentation: The space which is not used is not contiguous. It can be reduced by compaction.
- Internal fragmentation: Some portion of memory which is assigned to process is left unused.
A computer can address more memory than the amount physically installed on the system. This extra memory is actually called virtual memory and it is a section of a hard that's set up to emulate the computer's RAM. Paging technique plays an important role in implementing virtual memory.
Similarly, main memory is divided into small fixed-sized blocks of (physical) memory called frames and the size of a frame is kept the same as that of a page to have optimum utilization of the main memory and to avoid external fragmentation.
Page address is called logic address and represented by page number and the offset. \[ LogicalAddress = PageNumber + PageOffset \] Frame address is called physical address and represented by a frame number and the offset. \[ PhysicalAddress = FrameNumber + PageOffset \] A data structure called page map table is used to keep track of the relation between a page of a process to a frame in physical memory.
Here is a list of advantages and disadvantages of paging −
- Paging reduces external fragmentation, but still suffer from internal fragmentation.
- Paging is simple to implement and assumed as an efficient memory management technique.
- Due to equal size of the pages and frames, swapping becomes very easy.
- Page table requires extra memory space, so may not be good for a system having small RAM.
Segmentation is a memory management technique in which each job is divided into several segments of different sizes, one for each module that contains pieces that perform related functions. Each segment is actually a different logical address space of the program.
When a process is to be executed, its corresponding segmentation are loaded into non-contiguous memory though every segment is loaded into a contiguous block of available memory.
Segmentation memory management works very similar to paging but here segments are of variable-length where as in paging pages are of fixed size.
A program segment contains the program's main function, utility functions, data structures, and so on. The operating system maintains a segment map table for every process and a list of free memory blocks along with segment numbers, their size and corresponding memory locations in main memory. For each segment, the table stores the starting address of the segment and the length of the segment. A reference to a memory location includes a value that identifies a segment and an offset.