Memory Management in Operating System

FREE Online Courses: Click for Success, Learn for Free - Start Now!

In this tutorial, we will learn memory management in Operating systems and various ways for the same. Let’s start!!

What is Memory management in Operating System.

Memory management is a function of the OS. It helps us with:

  • managing the system memory (primary memory)
  • handling the movement of processes between main memory and disk during execution.
  • keeping track of every memory location, whether allocated or free
  • checking the amount of memory to be allocated to a process.
  • deciding which process will get memory and at what time
  • tracking when some memory is free or unallocated and updating its status

OS Memory Management Techniques

Following are the important memory management techniques:

1. Single Contiguous Allocation

This is the easiest memory management technique where all types of computer memories except the one reserved for the OS are available for one application.

2. Partitioned Allocation

In this allocation, the primary memory is divided into various memory partitions where each partition stores the information for a specific job. A job is given a partition when it starts executing and the partition is taken away after the job finishes execution.

3. Paged Memory Management

In this method, page frames are created which are fixed-size units formed after dividing the system memory.

4. Segmented Memory Management

This method is similar to the one above. It does not provide a linear and contiguous address space for a user program. Instead, there is a segment table that contains the physical address, size, and other data about the memory.

Process Address Space in OS

This is a set of logical addresses that a process mentions in its code. The OS maps the logical address to the physical address during memory allocation. Three types of addresses are used during allocation:

1. Symbolic address: This address is used in a source code. The basic elements of this address space are variable names, constants, and instruction labels.

2. Relative address: During compilation, the compiler converts symbolic address to relative address.

3. Physical address: The loader generates a physical address when a program is being loaded into main memory.

Virtual addresses and physical addresses are similar to each other in compile-time and load-time address-binding schemes but differ in execution-time address-binding scheme.

A set of logical addresses generated by a program is known as logical address space. Physical address space comprises of the set of physical addresses corresponding to logical addresses. Memory management unit (MMU) does the runtime mapping from virtual to physical address.

  • The MMU adds the value present in the base register to the addresses generated by a user process. This is then treated as offset when it is sent to the memory. For example, if the base register value is 5000, and the attempt made by the user to use address location is 10 then it will be dynamically reallocated to location 5010.
  • The user program deals with the virtual address. This cuts off any interaction with physical memory.

Static vs Dynamic Loading in OS

During the development of a computer program it is necessary to choose between static and dynamic loading. This is because if you decide to load a program statically, then during the time of compilation all the programs compile and link together with no external program or module dependency. The linker forms the object program with necessary object modules into an absolute program, including logical addresses.

In case of a dynamically loaded program, the compiler compiles the program and the modules that the user needs, their references are given and the rest is done during the time of execution.

With static loading, the program and data needs to load into memory, during loading, for the execution to start. With dynamic loading, a disk stores the dynamic routines of the library in a relocatable form. They are then loaded into memory only when needed by the program.

Static vs Dynamic Linking

In static linking, all the modules are combined as one into a single executable program by the linker, in order to avoid runtime dependency. Dynamic linking doesn’t require linking the actual module or library with the program, instead a reference to the dynamic module is provided during compilation and linking.

Swapping

Swapping is the mechanism of swapping(moving) a process out of main memory temporarily to the secondary storage(disk) so that that memory could be made available to other processes. Later, the system swaps the process back to the main memory. Though swapping affects the system performance, it also helps run multiple parallel processes.

This process is also known as a technique for memory compaction. The time taken for swapping processes is the time required to move the entire process to a secondary disk and then copy it back to the memory, plus the time taken by a process to regain the main memory.

Swapping in Memory Management

Memory Allocation in OS

The main memory has two parts:

  • Low Memory: This is the memory where the OS resides in this memory.
  • High Memory: This memory holds user processes.

The OS uses two memory allocation mechanisms:

1. Single-partition allocation: Relocation-register scheme protects user processes from each other, and makes sure that a process can’t change the OS code and related data. Value of the smallest physical address is present in this register whereas the logical addresses (every single one is less than the limit register) are present in the limit register.
2. Multiple-partition allocation: The main memory is divided into multiple partitions of fixed-size and each partition contains only one process. When a partition is free, the OS selects a process from the input queue so that it can be loaded into the free partition. When the process terminates, the partition is free for another process to use.

Partition Allocation

This method helps avoid internal fragmentation by dividing the memory into blocks (partitions) and allocates each process according to requirement. The partition allocation schemes are as follows:

  • First Fit: It allocates the process to the first block that is sufficient for it in the main memory.
  • Best Fit: It allocates the process to the first smallest block among the free partitions.
  • Worst Fit: It allocates the process to the largest available block in the main memory.
  • Next Fit: Similar to the first fit, it looks for the first sufficient block from the last allocation point.

Fragmentation in OS

The free memory space gets broken into little pieces when processes are loaded and removed from the system memory. But, after sometimes allocating processes to the memory blocks becomes difficult due to their small size because of this the memory blocks remain unused. This is known as Fragmentation. There are two types of fragmentation:

1. External Fragmentation: In this, the total memory space is large enough to satisfy a request and one can even reside a process in it, but due to not being close, it cannot be used.

2. Internal Fragmentation: In this, the memory block assigned to the process is bigger than the process. Due to this, some portion of the memory is left unused, since any other process cannot use it.
We can create free memory from fragmented memory using the compaction technique, in the following way.

Fragmentation in OS

To reduce external fragmentation one can use the compaction/shuffle memory contents and place all the free memory in one large block. To perform compaction feasibly, make sure that the relocation is dynamic.

For reducing internal fragmentation, assign the smallest partition to the process effectively. But, make sure that this partition is large enough for the process.

Paging in OS

Virtual memory is the extra amount of memory present in a system. This memory is not physically installed on the system and is a part of a hard disk set up to emulate the computer RAM. Paging helps in the implementation of virtual memory. It is a memory management technique where the process address space is broken into same-sized blocks called pages and the number of pages is used to measure the size of the process.

The main memory is also divided into small blocks of memory called frames. These frames are fixed-sized blocks and the size of each block is the same as that of a page. This ensures optimum utilization of the main memory and helps avoid external fragmentation.

Paging in OS

Address Translation

Page map table is a data structure that keeps track of the relations between a page of a process and a frame in physical memory.

  • Page address also known as logical address is represented by page number and offset. Thus, Logical Address = Page number + page offset
  • Frame address also known as physical address is represented by frame number and offset. Thus, Physical Address = Frame number + page offset

Address Translation in OS

When allocating a frame to any page, the system translates the logical address into a physical address, creating an entry into the page table which can be used throughout the process of execution. The system loads the corresponding pages of a process into the available memory frames during execution.

If a system runs out of RAM, the OS moves the idle or unwanted pages of a memory to the secondary memory. This frees up RAM for other processes to us and the OS also brings back these processes when the program needs them. This continues until the execution of a process gets complete.

Features of Paging

Following are the features of paging:

  • It reduces external fragmentation, though internal fragmentation is still present.
  • It is an efficient memory management technique that is easy to implement.
  • Swapping is easier because of the equal sizes of pages and frames.
  • Page table is not good for a system with a small ram as it requires extra memory space.

Segmentation

Segmentation is a memory management technique that divides each job into segments (logical address space) of multiple sizes. Each module has its own segment along with pieces that perform related functions. During execution, segments corresponding to the program are loaded into non-contiguous memory.

Segmentation is similar to paging, the only difference is that in segmentation, the length of segments is whereas in paging, all the pages have a fixed size.

The segment of a program contains the program’s main function, utility functions, data structures, and many more. The OS maintains a segment map table for every process and a list of free memory blocks that includes their segment numbers, size, and their memory locations in the main memory. It also stores the starting address and length of a segment.

Segmentation in OS

Summary

Memory management helps in the management of the system memory precisely the primary memory. It is a functionality of an OS. It is important that a user chooses between dynamic and static loading when a program is in the development phase. Swapping is moving a process from main memory to the secondary storage temporarily.

Fragmentation is the breaking down of a process into pieces when it is loaded into the system memory. Paging helps in the implementation of virtual memory. It is similar to segmentation but the difference between these two is that in segmentation the pieces are of variable size.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *