Memory Management - Segmentation Paging and Virtual Memory
Memory Management - Segmentation Paging and Virtual Memory
First of all you should know that in this case when we use the word memory we are referring to the RAM
only. A hard disk is not memory rather it is secondary storage.
What we must also understand is that RAM size is limited. For example you have a 8-GB RAM in your
computer, that means you cannot load more than 8-GB of data in the RAM, so you are limited.
EXAMPLE SCENARIO.
1. Suppose we have 4 programs A, B, C and D running on the computer system at the same time.
2. Program A is 4GB, Program B is 7GB , Program C is 3 GB and Program D is 1 GB in size.
3. The total size of all the programs is 4 + 7 + 3 + 1 = 15 GB
4. Whereas the RAM size in our computer is only 8GB.
5. Out of this 8GB ,some part of RAM size let’s say 1GB is used to hold the Operating System
currently in use which effectively leaves us with 7GB of space in the RAM.
6. So the programs require a total of 15GB whereas the RAM space we have available is only 7GB.
7. This simply means that the Operating System will not be able to load the entire 15GB into the
RAM at the same time. Rather OS will only load only parts of the programs into the RAM. In our
scenario only parts of program A, program B, program C and program D will be loaded into RAM.
The Operating System has several strategies to handle this problem. These memory management
techniques include
1. Segmentation
2. Paging
3. Virtual Memory
SEGMENTATION
(Note: Segmentation is not a very popular technique and is not widely used by Operating Systems)
How does the OS system know what the size of the segment is going to be and what do we mean by
a natural divide? (Please note that the explanation given below is only to help you grasp the concept,
it is not a potential exam question. The actual size calculation of a memory segment is much more
complex)
By natural divide we mean for example the different modules in a program code that perform a
specific task. So one module would form one natural divide and the size of each module may be
different from the other . Thus the program is divided naturally into parts based on the different
modules that carry out different tasks.
As far as allocating RAM memory is concerned the OS looks at the size of a module in a program
and allocates the corresponding amount of memory in the RAM according to the size of the module.
The picture shows all 4 different processes A,B,C,D loaded into the RAM. Each process has been assigned
a segment of memory according to the need of the program. Program C for example needed the most
space. Program D needed the least amount of space.
The segment table holds all the necessary information related to the segments in the memory.
PAGING
1. RAM Memory is divided into equal sized parts called page frames.
2. A process that is to run on the processor is divided into equal sized pages. The size of a page is
the same as the size of a page frame (i.e. if the page frame is 4KB , then the page size is also
4KB)
3. All the Pages of a process are loaded into page frames
(Note: This is not a very good approach. Loading a very large process into RAM will not leave room for
other processes to be loaded into the RAM and is not widely used by the Operating System)
PAGING WITH VIRTUAL MEMORY
1. RAM Memory is divided into equal sized parts called page frames.
2. A process that is to run on the processor is divided into equal sized pages. The size of a page is
the same as the size of a page frame (i.e. if the page frame is 4KB , then the page size is also
4KB)
3. Only pages that are immediately needed by the process are loaded into the RAM
4. Pages may be loaded discontinuously in memory
5. OS maintains a page table for each process in the memory
a. The page table stores index of pages
The following table is an incomplete page table for a process X that is running on the computer
135 0 0
1. Looking at this page table we can see that Process X has a total of 135 pages.
2. The presence flag indicates whether a page of the process is present in the RAM or not (1
indicating that the page is in the RAM and 0 indicating that the page is not present in the RAM.)
3. The page frame address (remember memory is divided into equal sized blocks called page
frames) indicates the page frame in the RAM where the process page is loaded e.g. Page 1 is
loaded in page frame number 132 and Page 2 of the process is loaded into page frame number
245.
4. This is what we mean by discontinuous i.e. if page 1 is in page frame 132 then it is not necessary
that page 2 is loaded continuously into the next page frame 133 rather it may be loaded
discontinuously in some other page frame such as 245.
5. Some pages are not present in the RAM
So if a process page is not present in the RAM where is it?
This is where the concept of virtual memory comes in. The remaining pages of the process are loaded in
a reserved area of the hard disk that acts as virtual memory .
1. Suppose CPU is currently executing instruction which was fetched from location number 700 in
the RAM.
2. The current instruction being executed is a jump instruction i.e. JMP 2500
3. CPU simply thinks next instruction to be fetched is lying at address 2500 in RAM
4. There are two possibilities now
a. The instruction is part of a page that is not continuously located, so even though the
instruction is in a page in the RAM, it may not be at location 2500 rather it may be
discontinuously located at some location like 8205 for example.
b. The second possibility is that the instruction is in a page which is not inside the RAM
rather that page is lying in virtual memory
5. To solve this problem of where the instruction is located (whether it is lying inside a page
present in the RAM or lying in a page present in the virtual memory) , a special hardware called
the Memory Management Unit calculates/determines the actual address of the instruction from
the address provided by CPU
6. If this page is not lying in the RAM it is fetched from the Virtual memory and loaded into RAM
a. What if there is no free page frame available right now in which to load this page?
i. The OS then decides which page in the RAM can be replaced using a page
replacement algorithm
1. One of the advantages of the virtual memory approach is that a very large program can be run
when an equally large amount of memory is unavailable
2. For the CPU, the address calculation is straightforward. It simply thinks all instructions of the
program are lying sequentially in the RAM. It does not have to perform complex address
calculations to determine the real address of the instruction.
3. One other advantage is that Virtual Memory allows only part of a program to be present in RAM
memory at one time. This allows RAM size to be shared amongst different processes that want
to run on the computer system.