0% found this document useful (0 votes)
9 views34 pages

Memory Management in Operating Systems

Chapter 3 discusses memory management, a critical function of operating systems that allocates and tracks memory resources to ensure efficient process execution and system performance. It covers key concepts such as swapping, virtual memory, page replacement algorithms, and segmentation, highlighting techniques for managing limited RAM and optimizing memory usage. The chapter also details various memory allocation methods and page replacement strategies to handle page faults effectively.

Uploaded by

aaydagi77
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)
9 views34 pages

Memory Management in Operating Systems

Chapter 3 discusses memory management, a critical function of operating systems that allocates and tracks memory resources to ensure efficient process execution and system performance. It covers key concepts such as swapping, virtual memory, page replacement algorithms, and segmentation, highlighting techniques for managing limited RAM and optimizing memory usage. The chapter also details various memory allocation methods and page replacement strategies to handle page faults effectively.

Uploaded by

aaydagi77
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

Chapter 3: Memory

Management
Contents
❖Basics of memory management
❖Swapping
❖Virtual memory
❖Page replacement algorithms
❖Segmentation

2
3.1 Basics of memory management
• Memory management is a core function of an operating

system (OS) that handles the computer’s primary (RAM) and

secondary (disk) memory resources.

• It ensures that memory is allocated efficiently to running

processes, maximizes overall system performance, and

prevents conflicts that could arise when multiple processes

attempt to access the same memory space.

• Efficient memory management is crucial because memory is

a limited and critical resource in a computer system.


3
Cont..
• Without proper management, processes could overwrite each
other’s data, leading to errors, crashes, or inefficient use of
system resources.
❖ The main responsibilities of memory management include:
• Allocation and deallocation of memory to processes as needed.
• Tracking used and free memory to optimize usage.
• Ensuring process isolation and protection, so one process
cannot access another’s memory illegally.
• Supporting multitasking, by allowing multiple programs to run
simultaneously without interfering with each other.
• Enabling virtual memory, which allows programs to use more
memory than physically available by temporarily storing data
on disk.
4
Cont..
❖ Memory Hierarchy
• Memory in a computer is organized in a hierarchical structure to
balance speed, cost, and capacity.
• The closer the memory is to the CPU, the faster and more
expensive it is.

5
Cont..
❖ Memory Allocation Methods
• There are mainly two types of memory allocation methods in an
operating system:
1. Contiguous Memory Allocation
2. Non-Contiguous Memory Allocation
a) Contiguous Memory Allocation
• Each process is assigned a single continuous block of memory.
• All the memory for the process is in adjacent addresses.
• It is simple to implement but may cause fragmentation.
• Example: If a process needs 50 KB, it gets one block of 50 KB in
memory:
• Process A: 50 KB
6
Cont..

7
Cont..
b) Non-Contiguous Memory Allocation

• A process is divided into smaller chunks/several parts that can be


stored in different locations of physical memory.

• These chunks do not need to be adjacent.

• It helps to reduce fragmentation and makes better use of available


memory but access may be slower if memory is scattered.

• The two main techniques used are Paging and Segmentation.

• Example: A process of 50 KB can be split into 5 blocks of 10 KB each,


placed anywhere in memory.
8
Cont..

9
3.2 Swapping
• Swapping is a process in which the operating system
moves data or programs between main memory and
secondary storage.

• It is used to manage limited RAM space and allows the


system to run more or larger programs at the same time.

• The CPU scheduler decides which processes should be


swapped in (brought into RAM) and which should be
swapped out (moved to secondary storage).

10
Cont..
• Swapping occurs only when the required data or process is
not available in RAM.

• This process may slow down system performance, because


accessing data from the hard disk is slower than from
RAM.

• However, it allows the system to run multiple processes


concurrently and improves overall memory utilization.

11
Cont..
• The below figure shows the swapping process in the operating
system:

12
Cont..
❖ Process of Swapping
• When the RAM becomes full and a new program needs to
run, the operating system selects an inactive process or data
currently in RAM.
• The selected process is moved (swapped out) to secondary
storage, creating free space in RAM.
• The new program is then loaded into the freed space.
• If the swapped-out process is needed again, it is swapped back
into RAM by replacing another inactive process if necessary.
• This continuous process of swapping in and swapping out
helps the system manage memory efficiently and allows
multiple programs to run even with limited RAM.

13
3.3 Virtual memory
• Virtual memory is a memory management technique used
by the operating system that allows a computer to use more
memory than the amount of physical RAM available by
temporarily storing data on the hard disk.

• It allows programs to use a large, continuous block of


memory, even if the actual memory is small.

• The OS divides programs into pages and stores parts of


them on the hard disk when there isn’t enough space in
RAM.
14
Cont..
❖ Objectives of Virtual Memory
• A program does not need to be fully loaded in memory to run
only required parts are loaded.
• Programs can be larger than the available physical memory.
• It creates an illusion of a large main memory, even on systems
with limited RAM.
• It uses both RAM and disk space to manage memory efficiently.
• Allows the system to run multiple programs at once and
improves overall system performance.
• Generally, virtual memory increases efficiency and multitasking
by using disk space as an extension of RAM.

15
Cont..

16
Cont..
❖ Types of Virtual Memory
• Virtual memory is managed by the Memory Management Unit
(MMU), usually built into the CPU.
• The CPU generates virtual addresses, and the MMU translates
them into physical addresses.
• There are two main types of virtual memory: Paging and
Segmentation.
❑ Paging
• Memory is divided into fixed-size blocks called pages.
• RAM is divided into frames of the same size.
• When a program runs, its pages are loaded into available frames
in RAM.
• Pages not currently in use are stored on the hard disk to free up
RAM.
17
Cont..
• When those pages are needed again, they are brought back
(swapped) into RAM.
• This allows large programs to run, even if they are bigger than
the available physical memory (RAM).
• Page Fault: Occurs when the CPU tries to access a page that is
not in RAM (it’s on the disk).
• The OS then loads that page from the disk into RAM.

18
3.4 Page replacement algorithm
• In a paging system, a page replacement algorithm is needed
when a page fault occurs and no free frame is available in RAM.
• The OS must replace an existing page with the new one.
• Common Page Replacement Techniques
1. First In First Out (FIFO)
2. Optimal Page Replacement
3. Least Recently Used (LRU)
4. Most Recently Used (MRU)
1. First In First Out (FIFO)
• The simplest page replacement method.
• Pages are kept in a queue based on the order they were loaded.
• The oldest page at the front of the queue is replaced first when a
new page is needed.
19
Cont..
• Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3
with 3-page frames. Find the number of page faults using
FIFO Page Replacement Algorithm.

20
Cont..
• Initially, all slots are empty, so when 1, 3, 0 came they are
allocated to the empty slots -> 3 Page Faults.

• When 3 comes, it is already in memory so -> 0 Page Faults.

• Then 5 comes, it is not available in memory, so it replaces the


oldest page slot i.e. 1. -> 1 Page Fault.

• 6 comes, it is also not available in memory, so it replaces the


oldest page slot i.e. 3 -> 1 Page Fault.

• Finally, when 3 come it is not available, so it replaces 0 ->


1-page fault.
21
Cont..
2. Optimal Page Replacement
• In this algorithm, pages are replaced which would not be used
for the longest duration of time in the future.

• This method minimizes the number of page faults.

❖ Example: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0,


3, 2, 3 with 4-page frame.

• Find number of page fault using Optimal Page Replacement


Algorithm.

22
Cont..

23
Cont..
• Initially, all slots are empty, so when 7 0 1 2 are allocated to the
empty slots -> 4 Page faults

• 0 is already there so -> 0 Page fault.

• when 3 came it will take the place of 7 because it is not used for
the longest duration of time in the future-> 1 Page fault.

• 0 is already there so -> 0 Page fault.

• 4 will takes place of 1 ---> 1 Page Fault.

• Remaining page references are already in memory → 0 page


faults.
24
Cont..
• Note:
• Optimal page replacement minimizes page faults.
• Not practical, because the OS cannot predict future page requests.
• It is used as a benchmark to compare other page replacement
algorithms.
3. Least Recently Used

• In this algorithm, page will be replaced which is least recently used. Or


• Remove the page that hasn't been used recently.

• Example: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0,


3, 2, 3 with 4-page frames.

• Find number of page faults using LRU Page Replacement Algorithm.

25
Cont..

26
Cont..
• Initially, all slots are empty, so when 7 0 1 2 are allocated to the
empty slots -> 4 Page faults.

• 0 is already there so -> 0 Page fault.

• when 3 came it will take the place of 7 because it is least recently


used -> 1 Page fault.

• 0 is already in memory so -> 0 Page fault.

• 4 will takes place of 1 -> 1 Page Fault.

• Now for the further page reference string -> 0 Page fault because
they are already available in the memory.
27
Cont..
4. Most Recently Used (MRU)

• In this algorithm, the page that was used most recently is


replaced first. Or
• Remove the page that was just used

• Belady’s anomaly (where increasing the number of page frames


increases page faults) can occur with this method.

• Example: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4,


2, 3, 0, 3, 2, 3 with 4-page frames.

• Find number of page faults using MRU Page Replacement


Algorithm. 28
Cont..

29
Cont..
• Initially, all slots are empty, so when 7 0 1 2 are allocated to the
empty slots ---> 4 Page faults
• 0 is already their so--> 0 page fault
• when 3 comes it will take place of 0 because it is most recently
used ---> 1 Page fault
• when 0 comes it will take place of 3 ---> 1 Page fault
• when 4 comes it will take place of 0 ---> 1 Page fault
• 2 is already in memory so ---> 0 Page fault
• when 3 comes it will take place of 2 ---> 1 Page fault
• when 0 comes it will take place of 3 ---> 1 Page fault
• when 3 comes it will take place of 0 ---> 1 Page fault
• when 2 comes it will take place of 3 ---> 1 Page fault
• when 3 comes it will take place of 2 ---> 1 Page fault: Total page 30
faults: 12
3.5 Segmentation
• Segmentation is a memory management technique where a
process is divided into variable-sized chunks called segments.

• Unlike paging, segmentation matches the logical view of a


program to physical memory.

❖ Segment Table
• A segment table stores information about each segment.
• It maps two-dimensional logical addresses (segment number +
offset) into one-dimensional physical addresses.

31
Cont..
• Each entry in the table contains:
• Base Address: Starting physical address of the segment in
memory.
• Segment Limit (Offset): Length of the segment.
• Segmentation matches logical program structure and improves
memory use, but it can cause fragmentation and slower access.

32
Cont..

33
Next Chapter 4
File Systems

You might also like