Memory Management
Memory Management
Page 1 of 14
Introduction
Computer memory, also termed primary memory, main memory or Random Access Memory
(RAM), is an important resource that must be very carefully managed. Thus, memory
management refers to management of main memory.
Main memory is a large array of bytes where each byte has its own address. It provides very
fast storage that can be accessed directly by the CPU. For a program to be executed, it must
be in the main memory.
The Operating system must perform the following tasks while managing memory
• Keep track of each memory location; what parts are in use, by whom, what areas are
free.
• It decides which processes get memory, when and how much memory.
• It monitors when memory is freed and updates memory status.
• Allocates memory when a process requests it to do so.
• De-allocates memory when a process no longer needs it or has been terminated.
Each memory location had its own unique address
102400
880000
Limit:459060
Process 3
420940
Limit:120900
Process 2
300040
Limit:44040
Process 1
256000 Base:256000
Operating
System
0
Page 2 of 14
The Operating system must ensure that each process stays within its allocated memory
address space. Thus, every time a process to read from or write to memory the base and limit
+ base must be verified.
Start Write to
Memory
Read Base
Address
Display Limit
Address Error
Read Limit
+
Base
No
End Write to
Write address Memory
>=
base Write address
Yes
< Yes Write to Memory
Base + Limit
No
Display Base
Address Error
So, to explain Figure 2. A call has come in to write to a memory address of a process. The
Base Address and Limit. The Write address is checked against the base address. It must be
greater than or equal to this value. If it is it can pass on to the Limit check, if not an error
message could be displayed. The Write address must be smaller than the base + limit. If it
is the write to memory operation can be performed, if not an error message could be displayed.
The runtime mapping from virtual address to physical address is done by the memory
management unit (MMU). The mapping process is as follows.
The value in the base register is added to every address generated by a user process
The value in the base register is used as an offset.
e.g., if the base register value is 10000, then an attempt to use address location within a
program of 100 will be dynamically reallocated to location 10100.
Page 3 of 14
A user program deals with logical addresses; it never sees the real physical addresses.
Process 1 RAM
0 ------------ 100 ----------
1 ------------ 101 ----------
2 ------------ 102 ----------
3 ------------ 103 ----------
4 ------------ +100 104 ----------
5 ------------ 105 ----------
6 ------------ 106 ----------
7 ------------ 107 ----------
8 ------------ 108 ----------
##############
##############
##############
##############
500
Process 2 501
502
0 ------------ 503
1 ------------ 504
2 ------------ +500
505
3 ------------ 506
4 ------------ 507
5 ------------ 508
6 ------------ ##############
7 ------------ ##############
8 ------------ ##############
##############
Figure 3. Logical Address To Physical Address Mapping
Page 4 of 14
User Space
Swap out
P1
Swap In P2
Operating System
Secondary Storage
Figure 4. Standard Swapping
P1
A Page Out
B
C
A B
P2
D
E
F Page In E F
Operating System
Secondary Storage
Figure 5. Paging
Page 5 of 14
Memory Allocation
To get a program executed it must be first placed in the memory (becoming a process).
Assigning space to a process in memory is called memory allocation. Allocation can either be
contiguous or non-contiguous
Each process is loaded into one and only one partition. No spanning of partitions is allowed.
As can be seen from the diagram there is a lot of fragmentation i.e. the unused space is wasted
as it cannot be used by any other processes. A further problem would be that if a process of
say 7MB came along it would not be able to run as even though there is technically enough
memory available, there is no open partition and further the available memory is not
contiguous
Page 6 of 14
Advantages of Fixed Partitions
• Easy to Implement
• Less overhead for Operating System
Page 7 of 14
Advantages of Variable Partitions
• No internal fragmentation
• No Process size limitation
• No limit on number of processes that can run (memory is not infinite)
• Difficult to implement
• More overhead on Operating System
Allocation Algorithms
The Operating system has to make a decision as to where to place a process in memory.
There are three main algorithms for allocating processes to memory partitions. We will use
the data below to illustrate each one
First Fit
The first partition found that can fit the process is chosen and the process is placed there.
Process 1 123 KB B
Process 2 323 KB E
Process 3 523 KB NULL
Process 4 50 KB A
Best Fit
All open partitions are checked an the one that is the closest match in size to the size of the
process is chosen and process placed there.
Process 1 123 KB C
Process 2 323 KB B
Process 3 523 KB E
Process 4 50 KB A
Worst Fit
The process is placed in the largest open partition.
Process 1 123 KB E
Process 2 323 KB B
Process 3 523 KB NULL
Process 4 50 KB D
Page 8 of 14
Fragmentation
Thus, as processes move in and out of memory, the free memory space is broken into little
pieces. It may happen that processes cannot be allocated to memory due to a lack of
contiguous memory. This problem is known as fragmentation.
External Fragmentation
Total memory space is enough to satisfy a request or to reside a process in it, but it is not
contiguous, so it cannot be used. External fragmentation can be reduced by a process
known as compaction. Compaction entails shuffling memory in such a manner that all free
memory becomes contiguous.
Free
Free
Free
Free
Used Used Used Used
Free
Used Used Used Used
Internal Fragmentation
Memory block assigned to process is bigger than the process requirements. Some portion of
memory is left unused and can thus not be used by another process.
Paging
Every logical address will now consist of two parts, a page number and an offset.
Similarly, each physical address will consist of a frame number and an offset.
Page 9 of 14
Let’s try and break this down. Assume this is a program that will be executed. What it does
is not important but if you are keen to know … basically it checks if a certain file exists. If
the file is found it reads through each line of the file. Each line contains 2 characters separated
by a comma. Each character is loaded into an array. If the file is not found an appropriate
error message is displayed.
Line# Instruction
0 var tFile : textfile; iNum,iPos : integer;
1 sLine : string; cChar : char;
2 Begin
3 if fileExists('draw.txt') then
4 Begin
5 assignFile(tFile,'Draw.txt');
6 reset(tFile);
7 iCount := 0;
8 while not eof(tFile) do
9 Begin
10 readln(tFile,sLine);
11 iPos := Pos(',',sLine);
12 cChar := copy(sLine,1,iPos-1)
13 Delete(sLine,1,iPos);
14win iNum := strToInt(sline);
15 inc(iCount);
16 arrCharacter[iCount] := cChar
17 arrNumber[iCount] := iNum;
18 end;
19 End
20 Else
21 Begin
22 showMessage('File Not Found');
23 end;
Table 1: Program Code
Page 10 of 14
If this program were going to execute in one contiguous block of physical memory the
Instruction number + base address would be sufficient to create the physical address (see
Figure 3. Logical Address To Physical Address Mapping) But we are going to implement
paging and when executing the program have the Process divided into 6 pages.
Page# Line# Offset Instruction
0 0 var tFile : textfile; iNum,iPos : integer;
1 1 sLine : string; cChar : char;
0
2 2 Begin
3 3 if fileExists('draw.txt') then
4 0 Begin
5 1 assignFile(tFile,'Draw.txt');
1
6 2 reset(tFile);
7 3 iCount := 0;
8 0 while not eof(tFile) do
9 1 Begin
2
10 2 readln(tFile,sLine);
11 3 iPos := Pos(',',sLine);
12 0 cChar := copy(sLine,1,iPos-1)
13 1 Delete(sLine,1,iPos);
3
14 2 iNum := strToInt(sline);
15 3 inc(iCount);
16 0 arrCharacter[iCount] := cChar
17 1 arrNumber[iCount] := iNum;
4
18 2 end;
19 3 End
20 0 Else
21 1 Begin
5
22 2 showMessage('File Not Found');
23 3 end;
Table 2: Logical Address Table
The Page# and the offset# now provides a logical address but creating/determining a physical
address requires an additional process. As stated previously physical memory will also be
divided into frames of the same size as the logical memory pages. A mapping mechanism is
required to determine where a logical address is located in physical memory, This is done
with a page table
Page# Frame#
0 2
1 5
2 0
3 6
4 8
5 3
Table 3: Page Table
If you look overleaf you will see that the code contained within each logical page can be found
at the corresponding physical frame. Using the same offset as seen in Table 2 one can
determine the complete physical address e.g. Line 17 is located logically on Page 4 offset 1.
Which maps physically to frame 8 offset 1.
Page 11 of 14
Frame# Physical Offset Instruction
Address
0 0 while not eof(tFile) do
1 1 Begin
0 readln(tFile,sLine);
2 2
3 3 iPos := Pos(',',sLine);
4 0
5 1
1
6 2
7 3
8 0 var tFile : textfile; iNum,iPos : integer;
9 1 sLine : string; cChar : char;
2 Begin
10 2
11 3 if fileExists('draw.txt') then
12 0 Else
13 1 Begin
3 showMessage('File Not Found');
14 2
15 3 end;
16 0
17 1
4
18 2
19 3
20 0 Begin
21 1 assignFile(tFile,'Draw.txt');
5 reset(tFile);
22 2
23 3 iCount := 0;
24 0 cChar := copy(sLine,1,iPos-1)
25 1 Delete(sLine,1,iPos);
6 iNum := strToInt(sline);
26 2
27 3 inc(iCount);
28 0
29 1
7
30 2
31 3
32 0 arrCharacter[iCount] := cChar
33 1 arrNumber[iCount] := iNum;
8 end;
34 2
35 3 End
36 0
37 1
9
38 2
39 3
Page 12 of 14
To Summarize. As a page is allocated to a frame, the logical page address must be translated
to a physical address and an entry stored in the page table.
When a process is to be executed, its corresponding pages are loaded into any available
memory frames and a page table created to map logical to physical addresses. Suppose you
have a program of 8Kb but your memory can accommodate only 5Kb at a given point in time,
then the paging can allow such a program to run because the entire program need not be in
memory but only the pages required at a given point in time. When a computer runs out of
RAM, the operating system (OS) will move idle or unwanted pages of memory to secondary
storage to free up RAM for other processes and bring pages back when required by the
program.
Advantages of Paging
• Paging reduces external fragmentation
• Paging is reasonably simple to implement and viewed as an efficient memory
management technique.
• Due to equal size of the pages and frames, swapping is easy.
Disadvantages of Paging
• Still suffer from internal fragmentation.
• Page table requires extra memory space, so may be problematic where RAM
is already low
Virtual Memory
A computer can address more memory than the amount of physically installed RAM. This
extra memory (virtual memory) is actually a section of a hard disk that is set up to emulate
RAM.
The main advantage of this is that programs can be larger than physical memory. Further,
user programs can use less physical memory, thus allowing more programs to be run at the
same time, with a corresponding increase in CPU utilization and throughput.
When might an entire program not be required to be fully loaded in RAM.
• User written error handling routines are required only when an error occurs.
• Certain options and features of a program may be used rarely.
• Many tables are assigned a fixed amount of address space even though only a small
It is likely that while executing, a program might reference a page which is not available in the
main memory because it was swapped to disk. This invalid memory reference is known as a
page fault. When a page fault occurs, the Operating System must determine which memory
page/s to swap out to disk to return the required pages to RAM. Page replacement algorithms
are the techniques used by an Operating System to make these paging decisions.
Page 13 of 14
Managing Virtual Memory Walkthroughs
https://www.windowscentral.com/how-change-virtual-memory-size-windows-10
https://www.tomshardware.com/news/how-to-manage-virtual-memory-pagefile-windows-
10,36929.html
https://www.geeksinphoenix.com/blog/post/2016/05/10/how-to-manage-windows-10-virtual-
memory
Bibliography
Silberschatz, A., Baer Galvin, P., & Gagne, G. (2018). Operating System Concepts. Hoboken: John
Wiley & Sons, Inc.
Tannenbaum, A., & Bos, H. (2015). Modern Operating Systems. New Jersey: Pearson.
W3Schools. (2019, 01 01). Operating System Tutorial Library. Retrieved 2022, from W3Schools:
https://www.w3schools.in/operating-system-tutorial
Page 14 of 14