0% found this document useful (0 votes)
6 views

Memory Management

Uploaded by

Sam Jam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Memory Management

Uploaded by

Sam Jam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MEMORY MANAGEMENT

Rutherford, Andrew (Mr) (Summerstrand Campus North)


NELSON MANDELA UNIVERSITY
TABLE OF CONTENTS
TABLE OF CONTENTS .......................................................................................................... 1
Figures and Tables ................................................................................................................. 1
Introduction ............................................................................................................................. 2
Memory Depiction and Memory Protection ............................................................................. 2
Logical vs Physical Addresses ................................................................................................ 3
Swapping and Paging ............................................................................................................. 4
Memory Allocation .................................................................................................................. 6
Contiguous Memory Allocation ...................................................................................................... 6
Fixed or Static Partitions ............................................................................................................. 6
Variable or Dynamic Partitions ................................................................................................... 7
Allocation Algorithms ................................................................................................................... 8
First Fit ....................................................................................................................................... 8
Best Fit ....................................................................................................................................... 8
Worst Fit..................................................................................................................................... 8
Fragmentation ................................................................................................................................... 9
External Fragmentation ............................................................................................................... 9
Internal Fragmentation ................................................................................................................ 9
Paging ................................................................................................................................................ 9
Advantages of Paging ............................................................................................................... 13
Disadvantages of Paging .......................................................................................................... 13
Bibliography .......................................................................................................................... 14

Figures and Tables


Figure 1. Simple Depiction of Memory .................................................................................................................... 2
Figure 2. Flowchart of Process Memory Address Protection .................................................................................. 3
Figure 3. Logical Address To Physical Address Mapping ....................................................................................... 4
Figure 4. Standard Swapping .................................................................................................................................. 5
Figure 5. Paging ...................................................................................................................................................... 5
Figure 6. Fixed Size Partitions ................................................................................................................................ 6
Figure 7. Dynamic Partitioning ................................................................................................................................ 7
Figure 8: Fragmented Memory ................................................................................................................................ 9
Figure 9: Memory after Compaction ........................................................................................................................ 9

Table 1: Program Code ........................................................................................................................................................ 10


Table 2: Logical Address Table........................................................................................................................................... 11
Table 3: Page Table ............................................................................................................................................................. 11

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

Memory Depiction and Memory Protection


All programs must be loaded into memory in order to execute. Once in memory we refer to
them as processes. Even the operating system must be loaded into memory. Each process
has a start address and an end address. The difference between the start address and the
end address is called the limit, which is basically the size of the process. The Base address
and Base Address + Limit must be stored in CPU registers

102400

880000
Limit:459060
Process 3

420940
Limit:120900
Process 2

300040
Limit:44040
Process 1

256000 Base:256000
Operating
System
0

Figure 1. Simple Depiction of Memory

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

Figure 2. Flowchart of Process Memory Address Protection

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.

Logical vs Physical Addresses


Every program has its own source code. Duh! Each program will have an address value of
each line of code e.g. 0 for line 1, 1 for line 2 etc. But each program will want to do the same
thing i.e. start their address values for lines of code from 0. Clearly though when we are
dealing with RAM their can only be one address value of 0. So how does one overcome this
problem of Processes having the same address values? The address of each line of code as
seen by the process is said to be a logical address. When processes are read into memory
each line of code is assigned a physical address in memory.

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

Swapping and Paging


Sometimes there may not be sufficient RAM to satisfy the memory requirements of all of the
currently executing processes in RAM. In such cases part of the process or the entire process
may be moved to secondary storage. If the entire process is moved to secondary storage it
is called swapping. The amount of time required to move an entire process between memory
and secondary storage is lengthy. Most modern operating systems segment processes into
different pages. This allows a page or pages of a process to be swapped out rather than the
entire process. This swapping out of page/s is called paging.

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

Contiguous Memory Allocation


Memory is usually divided into two partitions: one for the operating system and one for the
user processes. In contiguous allocation the entire program process is loaded into a single
contiguous memory location.

Fixed or Static Partitions


This is the oldest and simplest technique used to put more than one process into main
memory. The number of partitions (non-overlapping) in RAM is fixed but the size of each
partition may or may not be the same. Partitions are created during system configuration. It
is a contiguous allocation, i.e. no spanning is allowed. Here partitions are made before
execution or during system configure.

Figure 6. Fixed Size Partitions

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

Disadvantages of Fixed Partitions

• Internal Fragmentation (wasted space within each partition)


• External Fragmentation (Process cannot be accepted because not enough contiguous
free space)
• Process size limited to partition size
• Number of processes that can run is limited to number of partitions

Variable or Dynamic Partitions


Variable allocation can alleviate some of the problems faced by fixed partitioning. Partitions
are not made before the execution or during system configure. Initially RAM is empty, and
partitions are made during the run-time according to process’s need instead of partitioning
during system configuration. The size of partition will be equal to incoming process. Number
of partitions in RAM is not fixed and depends on the number of incoming process and Main
Memory’s size.

Figure 7. Dynamic Partitioning

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)

Disadvantages of Variable Partitions

• 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

Partition Size Process 1 123 KB


A 100 KB Process 2 323 KB
B 500 KB Process 3 523 KB
C 200 KB Process 4 50 KB
D 300 KB
E 600 KB

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

Figure 8: Fragmented Memory

Free
Used Used Used Used

Figure 9: Memory after Compaction

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

As reported earlier, contiguous memory allocation suffers from fragmentation, both


internal and external. Paging allows for non-contiguous memory allocation. In so
doing it avoids external fragmentation and the need for compaction. Paging is used
in most operating systems and is implemented through cooperation between the
operating system and the computer hardware.

The basic method for paging involves

• breaking physical memory into fixed-sized blocks (frames) and


• breaking logical memory into blocks of the same size (pages)

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.

Tutorialspoint. (2021, 01 01). Operating System Tutorial. Retrieved from Tutorialspoint.com:


https://www.tutorialspoint.com/operating_system/

W3Schools. (2019, 01 01). Operating System Tutorial Library. Retrieved 2022, from W3Schools:
https://www.w3schools.in/operating-system-tutorial

Page 14 of 14

You might also like