You are on page 1of 42

Faculty of Computer science

Operating System

Lecturer: Mr. Lutfullah


Haqnesar
Operating System

Chapter Five

Memory Management
Agenda
What is computer Memory
What is Memory?

• Computer memory, device that is used to store data or programs


(sequences of instructions) on a temporary or permanent basis for
use, in an electronic digital computer.

• On the basis of various functions, memory can be classified into


various categories.
• A computer device that is capable to store any information or data
temporally or permanently, is called storage device.
How Data is being stored in Memory?

• Machine understands only binary language that is 0 or 1.


• Computer converts every data into binary language first and then
stores it into the memory.
• If we have a program that stores an integer α = 10 then the
computer converts it into the binary language and then store it
into the memory blocks.
How Data is being stored in Memory?
Why we need Memory Management?
• The main purpose of a computer system is to execute programs.
• To improve both the utilization of the CPU and the speed of computer,
you must keep several processes in memory.

• Selection of a memory-management scheme for a system depends on


many factors, especially on the hardware design of the system.
Memory Management

Memory management is the functionality of an


operating system which handles or manages primary
memory.
Functions of Memory Management

1. To keeps track of each and every memory location either it is


allocated to some process or it is free.
2. It checks how much memory is to be allocated to processes.
3. It decides which process will get memory at what time.
4. It tracks whenever some memory gets freed and correspondingly it
updates the status.
5. Memory management provides protection by using two registers,
a base register and a limit register.
Base and Limit Register
Memory management provides protection by
using two registers, a base and a limit register.

1. Base: It is the base address of the


segment (contains the lowest address
allocated to P).

2. Limit: It is the length of the segment.


Base and Limit Register
Dynamic loading and dynamic linking

To gain proper memory utilization, dynamic loading and dynamic


linking is used.

1. Static loading:- loading the entire program into a fixed address. It


requires more memory space.

2. Dynamic loading:- In dynamic loading, a routine is not loaded until it


is called. All routines are residing on disk in a relocatable load format.
One of the advantages of dynamic loading is that unused routine is
never loaded.
Dynamic loading and dynamic linking

Dynamic linking:
In dynamic linking, “Stub” is included for each routine. A stub is a small
program that substitutes for a longer program, possibly to be loaded
later.
When the stub is executed, it checks whether the needed routine is
already in memory or not. If not available then the program loads the
routine into memory.
Memory Management strategies

1. Swapping

2. Fragmentation
Swapping
• Swapping is a process in which any process can be temporarily
swapped from main memory to secondary memory so that the main
memory can be made available for other processes.
• It is used to improve main memory utilization. In secondary memory,
the place where the swapped-out process is stored is called swap
space.

The purpose of the swapping in operating system is:


To access the data present in the hard disk and bring it to RAM.
Swapping is used only when data is not present in RAM
Swapping
Swapping
Example: Suppose the user process's size is 2048KB and is a standard hard
disk where swapping has a data transfer rate of 1Mbps.
• Now we will calculate how long it will take to transfer from main memory
to secondary memory.

User process size is 2048Kb


Data transfer rate is 1Mbps = 1024 kbps
Time = process size / transfer rate
= 2048 / 1024 = 2 seconds (2000 milliseconds)

• Now taking swap-in and swap-out time, the process will take 4000 milliseconds.
Advantages of Swapping

It helps the CPU to manage multiple processes within a single main
memory.

Swapping allows the CPU to perform multiple tasks simultaneously.

It improves the main memory utilization.


Fragmentation
• Fragmentation is an unwanted problem in the operating system in
which the processes are loaded and unloaded from memory, and free
memory space is fragmented.

• Processes can't be assigned to memory blocks due to their small size,


and the memory blocks stay unused.

• As the process is loaded and unloaded from memory, these areas are
fragmented into small pieces of memory that cannot be allocated to
coming processes.
Causes of Fragmentation
• User processes are loaded and unloaded from the main memory, and
processes are kept in memory blocks in the main memory.
• Many spaces remain after process loading and swapping that another
process cannot load due to their size.
• Main memory is available, but its space is insufficient to load another
process because of the dynamical allocation of main memory
processes.
Types of Fragmentation

There are mainly two types of fragmentation in the operating


system. These are as follows:

1. Internal Fragmentation

2. External Fragmentation
Types of Fragmentation

1. Internal Fragmentation
• When a process is allocated to a memory block, and if the process is
smaller than the amount of memory requested, a free space is
created in the given memory block.
• Due to this, the free space of the memory block is unused, which
causes internal fragmentation.
Example
How to avoid internal fragmentation?

• The problem of internal fragmentation may arise due to the fixed


sizes of the memory blocks.
• It may be solved by assigning space to the process via dynamic
partitioning.
• Dynamic partitioning allocates only the amount of space requested by
the process. As a result, there is no internal fragmentation.
Types of Fragmentation

2. External Fragmentation
• External fragmentation happens when a dynamic memory allocation
method allocates some memory but leaves a small amount of
memory unusable.
• The quantity of available memory is substantially reduced if there is
too much external fragmentation.
• There is enough memory space to complete a request, but it is not
contiguous. It's known as external fragmentation.
Example
How to avoid external fragmentation?

• This problem occurs when you allocate RAM to processes


continuously.
• It is done in paging and segmentation, where memory is allocated to
processes non-contiguously.
• As a result, if you remove this condition, external fragmentation may
be decreased.
Memory Allocation Techniques

There are two types of Memory allocation techniques:

1. Contiguous memory allocation

2. None contiguous Memory allocation


1. Contiguous Memory Allocation
• A contiguous memory allocation is a memory management technique
where whenever there is a request by the user process for the
memory, a single section of the contiguous memory block is given to
that process according to its requirement.

• In the Contiguous Memory Allocation, each process is contained in a


single contiguous section of memory.
1. Contiguous Memory Allocation
• In this memory allocation, all the available memory space remains
together in one place which implies that the freely available memory
partitions are not spread over here and there across the whole
memory space.

• The memory can be divided either in the fixed-sized partition or in


the variable-sized partition.
1. Contiguous Memory Allocation
1.1 Fixed Partitioning
• In Fixed Partitioning, the main memory is divided into partitions of
equal or different sizes. The operating system always resides in the
first partition while the other partitions can be used to store user
processes. The memory is assigned to the processes in contiguous
way.
In fixed partitioning:
The partitions cannot overlap.
A process must be contiguously present in a partition for the
execution.
1.1 Fixed Partitioning(con..)
There are various cons of using this technique.
1. Internal Fragmentation
• If the size of the process is lesser then the total size of the partition
then some size of the partition get wasted and remain unused. This is
wastage of the memory and called internal fragmentation.

2. External Fragmentation
• The total unused space of various partitions cannot be used to load
the processes even though there is space available but not in the
contiguous form.
1.1 Fixed Partitioning(con..)
1.1 Fixed Partitioning(con..)
3. Limitation on the size of the process
If the process size is larger than the size of maximum sized partition
then that process cannot be loaded into the memory.

4. Degree of multiprogramming is less


By Degree of multi programming, we simply mean the maximum
number of processes that can be loaded into the memory at the same
time. In fixed partitioning, the degree of multiprogramming is fixed.
1.2 Dynamic Partitioning
• In Dynamic Partitioning, the main memory is divided into different
partitions where the partitions size is not declared initially. It is
declared at the time of process loading.
• The first partition is reserved for the operating system. The remaining
space is divided into parts. The size of each partition will be equal to
the size of the process.
• Dynamic partitioning tries to overcome the problems caused by fixed
partitioning.
1.2 Dynamic Partitioning
Advantages of Dynamic Partitioning

1. No Internal Fragmentation
• In dynamic partitioning there will not be any internal fragmentation because
there will not be any unused remaining space in the partition.
2. No Limitation on the size of the process
• In Dynamic partitioning, the process size can't be restricted since the partition
size is decided according to the process size.
3. Degree of multiprogramming is dynamic
• There will not be any unused space in the partition hence more processes can be
loaded in the memory at the same time.
Disadvantages of dynamic partitioning

External Fragmentation
• Absence of internal fragmentation doesn't mean that there will not
be external fragmentation.

Complex Memory Allocation


• The allocation and deallocation is very complex since the partition size
will be varied every time.
Disadvantages of dynamic partitioning

You might also like