Memory Management
What is Main Memory?
The main memory is central to the operation of a Modern Computer. Main Memory is a large array of words or bytes, ranging in size from hundreds of thousands to billions. Main memory is a repository of rapidly available information shared by the CPU and I/O devices. Main memory is the place where programs and information are kept when the processor is effectively utilizing them.Memory is associated with the processor, so moving instructions and information into and out of the processor is extremely fast. Main memory is also known as RAM(Random Access Memory. This memory is volatile. RAM loses its data when a power interruption occurs.
![Main Memory](https://media.geeksforgeeks.org/wp-content/uploads/20221116104505/1white-660x453.png)
What is Memory Management?
In a multiprogramming computer, the Operating System resides in a part of memory, and the rest is used by multiple processes. The task of subdividing the memory among different processes is called Memory Management. Memory management is a method in the operating system to manage operations between main memory and disk during process execution. The main aim of memory management is to achieve efficient utilization of memory.
Why Memory Management is Required?
- Allocate and de-allocate memory before and after process execution.
- To keep track of used memory space by processes.
- To minimize fragmentation issues.
- To proper utilization of main memory.
- To maintain data integrity while executing of process.
Logical and Physical Address Space
- Logical Address Space: An address generated by the CPU is known as a “Logical Address”. It is also known as a Virtual address.Logical address space can be defined as the size of the process. A logical address can be changed.Physical Address Space: An address seen by the memory unit (i.e the one loaded into the memory address register of the memory) is commonly known as a “Physical Address”.
- A Physical address is also known as a Real address. The set of all physical addresses corresponding to these logical addresses is known as Physical address space. A Physical address is computed by MMU. The run-time mapping from virtual to physical addresses is done by a hardware device Memory Management Unit(MMU). The physical address always remains constant.Static and Dynamic Loading
Loading a process into the main memory is done by a loader. There are two different types of loading :
- Static Loading: Static Loading is basically loading the entire program into a fixed address. It requires more memory space.
- Dynamic Loading: The entire program and all data of a process must be in physical memory for the process to execute. So, the size of a process is limited to the size of physical memory To gain proper memory utilization, dynamic loading is used. In dynamic loading a routine is not loaded until it is called. All routines are residing on disk in a relocatable load format. One of the advantages of dynamic loading is that the unused routine is never loaded. This loading is useful when a large amount of code is needed to handle it efficiently.
Static and Dynamic Linking
To perform a linking task a linker is used. A linker is a program that takes one or more object files generated by a compiler and combines them into a single executable file.
- Static Linking: In static linking the linker combines all necessary program modules into a single executable program. So there is no runtime dependency. Some operating systems support only static linking, in which system language libraries are treated like any other object module.
- Dynamic Linking: The basic concept of dynamic linking is similar to dynamic loading. In Dynamic linking “Stub” is included for each appropriate library routine reference. A stub is a small piece of code. When the stub is executed, it checks whether the needed routine is already in memory or not. If not available then the program loads the routine into memory.
Swapping
When a process is executed it must have resided in memory. Swapping is a process of swapping a process temporarily into a secondary memory from the main memory, which is fast compared to secondary memory. A swapping allows more processes to be run and can be fit into memory at one time. The main part of swapping is transferred time and the total time is directly proportional to the amount of memory swapped Swapping is also known as roll-out, or roll because if a higher priority process arrives and wants service, the memory manager can swap out the lower priority process and then load and execute the higher priority process. After finishing higher priority work, the lower priority process swapped back in memory and continued to the execution process.
![swapping in memory management](https://media.geeksforgeeks.org/wp-content/uploads/20221116104533/2white-660x441.png)
Memory Management with Monoprogramming (Without Swapping)
This is the simplest memory management approach the memory is divided into two sections:
- One part of the operating system
- The second part of the user program
Fence Register | |
---|---|
operating system | user program |
- In this approach, the operating system keeps track of the first and last location available for the allocation of the user program
- The operating system is loaded either at the bottom or at top
- Interrupt vectors are often loaded in low memory therefore, it makes sense to load the operating system in low memory
- Sharing of data and code does not make much sense in a single process environment
- The Operating system can be protected from user programs with the help of a fence register.
Advantages of Memory Management
- It is a simple management approach
Disadvantages of Memory Management
- It does not support multiprogramming.
- Memory is wasted
Multiprogramming with Fixed Partitions (Without Swapping)
- A memory partition scheme with a fixed number of partitions was introduced to support multiprogramming. this scheme is based on contiguous allocation
- Each partition is a block of contiguous memory
- Memory is partitioned into a fixed number of partitions.
- Each partition is of fixed size
Example: As shown in fig. memory is partitioned into 5 regions the region is reserved for updating the system the remaining four partitions are for the user program.
Fixed Size Partitioning
Operating System |
---|
p1 |
p2 |
p3 |
p4 |
Partition Table
Once partitions are defined operating system keeps track of the status of memory partitions it is done through a data structure called a partition table.
Sample Partition Table
Starting Address of Partition | Size of Partition | Status |
---|---|---|
0k | 200k | allocated |
200k | 100k | free |
300k | 150k | free |
450k | 250k | allocated |
Logical vs Physical Address
An address generated by the CPU is commonly referred to as a logical address. the address seen by the memory unit is known as the physical address. The logical address can be mapped to a physical address by hardware with the help of a base register this is known as dynamic relocation of memory references.
Contiguous Memory Allocation
The main memory should accommodate both the operating system and the different client processes. Therefore, the allocation of memory becomes an important task in the operating system. The memory is usually divided into two partitions: one for the resident operating system and one for the user processes. We normally need several user processes to reside in memory simultaneously. Therefore, we need to consider how to allocate available memory to the processes that are in the input queue waiting to be brought into memory. In adjacent memory allotment, each process is contained in a single contiguous segment of memory.
![Contiguous Memory Allocation](https://media.geeksforgeeks.org/wp-content/uploads/20221116104926/3white.png)
Memory Allocation
To gain proper memory utilization, memory allocation must be allocated efficient manner. One of the simplest methods for allocating memory is to divide memory into several fixed-sized partitions and each partition contains exactly one process. Thus, the degree of multiprogramming is obtained by the number of partitions.
- Multiple partition allocation: In this method, a process is selected from the input queue and loaded into the free partition. When the process terminates, the partition becomes available for other processes.
- Fixed partition allocation: In this method, the operating system maintains a table that indicates which parts of memory are available and which are occupied by processes. Initially, all memory is available for user processes and is considered one large block of available memory. This available memory is known as a “Hole”. When the process arrives and needs memory, we search for a hole that is large enough to store this process. If the requirement is fulfilled then we allocate memory to process, otherwise keeping the rest available to satisfy future requests. While allocating a memory sometimes dynamic storage allocation problems occur, which concerns how to satisfy a request of size n from a list of free holes. There are some solutions to this problem.
First Fit
In the First fit the first available free hole fulfil the requirement of the process allocated.
![Best Fit](https://media.geeksforgeeks.org/wp-content/uploads/20200524132547/BEST-FIT-300x225.png)
Here in this example, first, we traverse the complete list and find the last hole 25KB is the best suitable hole for Process A(size 25KB). In this method, memory utilization is maximum as compared to other memory allocation techniques.
Worst Fit
In the worst fit allocate the largest available hole to process. This method produces the largest leftover hole.
![Worst Fit](https://media.geeksforgeeks.org/wp-content/uploads/20200524132634/WORST-FIT-300x225.png)
Worst Fit
Here in this example, Process A (Size 25 KB) is allocated to the largest available memory block which is 60KB. Inefficient memory utilization is a major issue in the worst fit.