techtud's picture
Introduction to Memory management and virtual memory

Operating System reside in part of memory and rest is used by multiple processes. A process needs some memory to store its variable and code. Operating system must manages memory location, either allocated or free. The efficient utilization of memory by minimizing internal and external fragmentation is the goal of memory management and it’s functionality is the allocating and deallocating memory to processes.


Main Memory refers to a physical memory that is the internal memory of the computer. The word main is used to distinguish it from external mass storage devices such as disk drives. Main memory is also known as RAM. The computer is able to change only data that is in main memory. Therefore, every program we execute and every file we access must be copied from a storage device into main memory.

All the programs are loaded in the main memeory for execution. Sometimes the complete program is loaded into the memory, but some times a certain part or routine of the program is loaded into the main memory only when it is called by the program, this mechanism is called Dynamic Loading, this enhances the performance.

Also, at times one program is dependent on some other program. In such a case, rather than loading all the dependent programs, CPU links the dependent programs to the main executing program when its required. This mechanism is known as Dynamic Linking.


Virtual Memory

Virtual Memory is a space where large programs can store themselves in form of pages while their execution and only the required pages or portions of processes are loaded into the main memory. This technique is useful as large virtual memory is provided for user programs when a very small physical memory is there.

In real scenarios, most processes never need all their pages at once, for following reasons :

  • Error handling code is not needed unless that specific error occurs, some of which are quite rare.
  • Arrays are often over-sized for worst-case scenarios, and only a small fraction of the arrays are actually used in practice.
  • Certain features of certain programs are rarely used.


Benefits of having Virtual Memory :

  1. Large programs can be written, as virtual space available is huge compared to physical memory.
  2. Less I/O required, leads to faster and easy swapping of processes.
  3. More physical memory available, as programs are stored on virtual memory, so they occupy very less space on actual physical memory.

Logical vs physical address space

The address generated by CPU is called logical (or virtual) address space. Processes are always uses virtual address space and they do not see physical address. Logical address space is set of logical addresses that generated by a program.

The physical address is address that seen by memory unit and used to access memory units. Virtual addresses are mapped with physical addresses by memory management unit.




Objective and Functions of MMU:

  • Memory management unit (MMU) is a hardware device that maps virtual addresses to physical addresses.
  • The functions of the memory management unit is raising exception when necessary, protection and translation of virtual to physical addresses. Operating system handles memory management unit



Contributor's Info

Created: Edited:
Schemes of Memory Allocation

Memory allocation techniques are assigning of blocks of memory on request to the processes. Main memory is divided into set of non-overlapping memory regions called partitions that can be allocated in contiguous or non - contiguous partitions. The advantages of memory allocation techniques is simple and no hardware supports required, but there are some disadvantages like program size is fixed, wastage of CPU time, and main memory not utilized fully etc.


There are two types of partitions:


  • Contiguous partition, which is divided into two categories, i.e., Single partitions and multiple(fixed and variable) partitions.

  • Non contiguous partition, which is divided into three categories, i.e., Paging, segmentation, and segmented paging scheme.


Single Partition Allocation

This is simple to implement, however inefficient and program size is fixed. The main memory is divided into two parts for operating system and for user program that is running. That means only one user with one process is present in the system.


    47 (3).jpg


Multiple Partition Allocation

User memory is divided into more than one partitions. There are two types fixed partition or static partition and variable partition or dynamic partition.


  • Fixed partition has equal or unequal size of partitions and a partition is allocated to an active process in the multiprogramming system, however it may suffers from internal and external fragmentations, it supports only fixed number of active processes.

  • Variable partition  has equal or unequal size of partitions and these partitions are created dynamically. A block or hole is available memory or free partitions. Variable partition has no internal fragmentation and it better utilization of memory than fixed partition. However, variable partition suffers from external fragmentation and it has overhead of compaction.


Dynamic Allocation Algorithms

If more than one empty hole or blocks are available for an active process then the operating system decides how to select a best hole or block using using dynamic allocation algorithms. These algorithms are as following:


  • First fit: It allocated the first hole that is big enough to process, this started from the beginning to search such empty hole, so first fit algorithm is simple to implement and faster to use.

  • Best fit: This allocates smallest hole that is big enough for process. This is fast when all empty blocks or holes sorted in increasing order of size. Best fit minimize internal fragmentation.

  • Worst fit: This allocates largest hole that is empty for process. This creates maximum internal fragmentation. Worst fit is fast when all empty blocks or holes sorted in decreasing order of size.

  • Next fit: This allocates first empty block or hole that big enough for process. Next fit starts searching from where it left off, not from  the beginning. This works same as first fit algorithm.


Contributor's Info

Internal and External Fragmentation

Memory is divided in blocks. Fragmentation is the phenomena where a block or hole of memory is wasted that can not be used.


Internal Fragmentation

When a program is allocated to a memory block, if that program is lesser than this memory block and remaining space goes wasted, this situation is called internal fragmentation. Generally, internal fragmentation memory partition is static or fixed.




External Fragmentation

When total memory is enough available to a process but can not be allocated because of memory blocks are very small. That means program size is more than any available memory hole. Generally, external fragmentation occurs in dynamic or variable size partitions. External fragmentation can be solved using compaction technique. Also external fragmentation can be prevented by paging or segmentation mechanisms.


Contributor's Info

Variable size | FIRST | BEST | WORST fit
Content covered: 

 Variable size | FIRST | BEST | WORST fit

More Less
Fixed size partitioning | FIRST | BEST | WORST
Content covered: 

Fixed size partitioning | FIRST | BEST | WORST

More Less