0% found this document useful (0 votes)
34 views55 pages

OS 4th

Memory management is a crucial operating system function that allocates, deallocates, and protects memory for processes, ensuring efficient utilization of limited resources. Techniques include contiguous and non-contiguous memory management, with methods like fixed and variable partitioning, paging, and segmentation, each having advantages and disadvantages. Virtual memory enhances system performance by allowing larger processes to run on limited physical memory, while demand paging optimizes memory usage by loading pages only when needed.

Uploaded by

sakshamdubey022
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)
34 views55 pages

OS 4th

Memory management is a crucial operating system function that allocates, deallocates, and protects memory for processes, ensuring efficient utilization of limited resources. Techniques include contiguous and non-contiguous memory management, with methods like fixed and variable partitioning, paging, and segmentation, each having advantages and disadvantages. Virtual memory enhances system performance by allowing larger processes to run on limited physical memory, while demand paging optimizes memory usage by loading pages only when needed.

Uploaded by

sakshamdubey022
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/ 55

Memory Management

Aditi Samadhiya
Assistant Professor, MITS Gwalior
PhD Scholar, ABV IIITM Gwalior
Memory Management
• Module of OS which manages the memory.
• Memory is the important part of the computer that is used to store the
data. Its management is critical to the computer system because the
amount of main memory available in a computer system is very
limited. At any time, many processes are competing for it.
• Moreover, to increase performance, several processes are executed
simultaneously. For this, we must keep several processes in the main
memory, so it is even more important to manage them effectively.
Function of Memory Management
1. Memory allocation:-
• When a process comes from new state to ready state or from hard
disk to main memory. Then the allocation of process in main memory
is decided by memory management.
2. Memory deallocation:-
• When a process completes its execution then the space it occupied
should be deallocated by memory deallocation.
3. Memory Protection:-
• Protection is needed so that process can access its own area not
other process area.
Goals of Memory Management
• Minimum wastage and Maximum utilization of space.
• Ability to run larger programs with limited space.
Memory Management Techniques
Contiguous Non-Contiguous
Entire process should be stored in Process can not stored on
Consecutive location. Consecutive location.
1. Fixed Partition 1. Paging
2. Variable Partition 2. Segmentation
Contiguous Memory Management
• Entire process should be stored on consecutive location.
• In a Contiguous memory management scheme, each program occupies
a single contiguous block of storage locations, i.e., a set of memory
locations with consecutive addresses.
Fixed Partitioning

• The main memory is divided into fixed no. of partition and each
partition can be used to accommodate maximum one process.
• The main memory is divided into several fixed-sized partitions in a
fixed partition memory management scheme or static partitioning.
• These partitions can be of the same size or different sizes. Each
partition can hold a single process.
• The number of partitions determines the degree of multiprogramming,
i.e., the maximum number of processes in memory. These partitions
are made at the time of system generation and remain fixed after that.
Fixed Partitioning: Allocate P1 to this
partition
OS
150KB
200KB
100KB
60KB
Partition allocation policy
• When a new process arrive, then that process should get space where
and in which partition is decided by the operating system and
memory management techniques.
1. First Fit
2. Best Fit
3. Worst Fit
4. Next Fit
Internal Fragmentation
• If extra space is allocated to a process more than required space.
Hence, wastage of that extra space is known as Internal
Fragmentation.
Fixed Partitioning
• Advantages of Fixed Partitioning memory management schemes:
• Simple to implement.
• Easy to manage and design.
• Disadvantages of Fixed Partitioning memory management
schemes:
• This scheme suffers from internal fragmentation.
• The number of partitions is specified at the time of system generation.
Variable Partition Contiguous memory
management technique
• Main memory is not divided into partition initially.
• When a new process arrives, a new partition is created of same size
and the process is allocated into that partition.
• Hence, no any internal fragmentation.
Variable Partition
OS OS OS OS
P1 P1 P1
P2 P2
P3

600KB P1, Size = 150kb P2, Size = 100kb P3,Size = 200kb


OS
P1

P3

P2 completes its execution and P4 of size 200 kb arrives.


External Fragmentation
• If enough space is available t store a process but not consecutive.
Hence, wastage of space is known as External Fragmentation.
External Fragmentation:- Solution is
Compaction
• It collect entire allocated processes into one side of memory, so that
other side of memory can have entire free space.
• But it is time consuming process.
OS OS OS
P1 P1 P1
P3 P3
100kb P4
P3
150kb
Variable Partition
• Advantages of Variable Partitioning memory management
schemes:
• Simple to implement.
• Easy to manage and design.
• Disadvantages of Variable Partitioning memory management
schemes:
• This scheme also suffers from internal fragmentation.
• The number of partitions is specified at the time of system
segmentation.
Non- Contiguous Memory Management
Techniques
• Process is scattered in memory, not allocated in one area.
• Two techniques:
1. Paging
• Scattered in same size of memory areas.
• Equal size partition.
1. Segmentation
• Scattered in variable size of memory areas.
• Variable size partition.
Paging
• In Operating Systems, Paging is a storage mechanism used to retrieve
processes from the secondary storage into the main memory in the form of
pages.
• The main idea behind the paging is to divide each process in the form of
pages. The main memory will also be divided in the form of frames.
• One page of the process is to be stored in one of the frames of the memory.
The pages can be stored at the different locations of the memory but the
priority is always to find the contiguous frames or holes.
• Pages of the process are brought into the main memory only when they are
required otherwise, they reside in the secondary storage.
• Different operating system defines different frame sizes. The sizes of each
frame must be equal. Considering the fact that the pages are mapped to the
frames in Paging, page size needs to be as same as frame size.
Paging
Paging: Example

• Let us consider the main memory size 16 Kb and Frame size is 1 KB


therefore the main memory will be divided into the collection of 16
frames of 1 KB each.
• There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB
each. Each process is divided into pages of 1 KB each so that one page
can be stored in one frame.
• Initially, all the frames are empty therefore pages of the processes will
get stored in the contiguous way.
• Frames, pages and the mapping between the two is shown in the image
below.
Paging: Example
Paging Question
1. Calculate the size of memory if its address consists of 22 bits and the
memory is 2-byte addressable.
Solution-

We have-
Number of locations possible with 22 bits = 222 locations
It is given that the size of one location = 2 bytes

Thus, Size of memory


= 222 x 2 bytes
= 223 bytes
= 8 MB
Paging Question
2. Calculate the number of bits required in the address for
memory having size of 16 GB. Assume the memory is 4-byte
addressable.
Solution-
Let ‘n’ number of bits are required. Then, Size of memory = 2n x 4
bytes.
Since, the given memory has size of 16 GB, so we have-
2n x 4 bytes = 16 GB
2n x 4 = 16 G
2n x 22 = 234
2n = 232
∴ n = 32 bits
Segmentation
• A process is divided into Segments. The chunks that a program is
divided into which are not necessarily all of the exact sizes are called
segments.
• Segmentation gives the user’s view of the process which paging does
not provide. Here the user’s view is mapped to physical memory.
What is Segment Table?

• It maps a two-dimensional Logical address into a


one-dimensional Physical address. It’s each table entry has:
• Base Address: It contains the starting physical address where
the segments reside in memory.
• Segment Limit: Also known as segment offset. It specifies the
length of the segment.
What is Segment Table?
Advantages/Disadvantages of
Segmentation
• Advantages of Segmentation in Operating System
• Reduced Internal Fragmentation : Segmentation can reduce
internal fragmentation compared to fixed-size paging, as
segments can be sized according to the actual needs of a
process.
• Disadvantages of Segmentation in Operating System
• External Fragmentation : As processes are loaded and
removed from memory, the free memory space is broken into
little pieces, causing external fragmentation. This is a notable
difference from paging, where external fragmentation is
significantly lesser.
Virtual Memory
• Virtual Memory is a storage scheme that provides user an illusion of having
a very big main memory. This is done by treating a part of secondary
memory as the main memory.
• In this scheme, User can load the bigger size processes than the available
main memory by having the illusion that the memory is available to load the
process.
• Instead of loading one big process in the main memory, the Operating
System loads the different parts of more than one process in the main
memory.
• By doing this, the degree of multiprogramming will be increased and
therefore, the CPU utilization will also be increased.
Virtual Memory
Page 0
Page 1 Page 2
Page 2 0 Page 7
Page 0 Page 1
Page 3 Page 4 Page 3 Page 6
Page 4 2 Page 5
Page 5 3
Page 6 Physical Memory
(Frames)
Page 7 1
Secondary Memory
Process (8pages)
Page Table
Virtual Memory
• Advantages of Virtual Memory
1. The degree of Multiprogramming will be increased.
2. User can run large application with less real RAM.
3. There is no need to buy more memory RAMs.
• Disadvantages of Virtual Memory
1. The system becomes slower since swapping takes time.
2. It takes more time in switching between applications.
3. The user will have the lesser hard disk space for its use.
Demand Paging
• According to the concept of Virtual Memory, in order to execute some process,
only a part of the process needs to be present in the main memory which means
that only a few pages will only be present in the main memory at any time.
• However, deciding, which pages need to be kept in the main memory and which
need to be kept in the secondary memory, is going to be difficult because we
cannot say in advance that a process will require a particular page at particular
time.
• Therefore, to overcome this problem, there is a concept called Demand Paging is
introduced. It suggests keeping all pages of the frames in the secondary memory
until they are required. In other words, it says that do not load any page in the main
memory until it is required.
• Whenever any page is referred for the first time in the main memory, then that page
will be found in the secondary memory.
What is a Page Fault?

• If the referred page is not present in the main memory, then there will
be a miss and the concept is called Page miss or page fault.
• The CPU has to access the missed page from the secondary memory.
If the number of page fault is very high then the effective access time
of the system will become very high.
Page Replacement Policies
• In case of page fault, OS brings faulted page from secondary memory
to main memory by using page replacement algorithm (if needed)
and update page table.
Page Replacement Policies
1. First In First Out (FIFO)
2. Optimal Policy
3. Least Recently Used (LRU)
First In First Out (FIFO)

• Replace that page which is brought to main memory first.


• Assume:
1. No. of frames = 3 (all empty initially)
2. Page reference sequence = 1,2,3,4,1,2,5,1,2,3,4,5
3. Find out the number of page fault ?
First In First Out (FIFO)
First In First Out (FIFO)

• Replace that page which is brought to main memory first.


• Assume:
1. No. of frames = 4 (all empty initially)
2. Page reference sequence = 1,2,3,4,1,2,5,1,2,3,4,5
3. Find out the number of page fault ?
First In First Out (FIFO)
Belady’s Anomaly
• When you increase the number of frames, the page fault also
increases but with few page reference sequence.
First In First Out (FIFO)
Advantages
• Simple and easy to implement.

Disadvantages
• Poor performance
• Suffers from Belady’s Anomaly.
Optimal Policy
• Replace page which is not (never) going to be referred in near future.
• Assume:
1. No. of frames = 4 (all empty initially)
2. Page reference sequence = 1,2,3,4,1,2,5,1,2,3,4,5
3. Find out the number of page fault ?
Optimal Policy
Optimal Policy
Advantages
• Easy to implement
• Highly efficient

Disadvantages
• Requires future knowledge of the program
• Time-consuming
Least Recently Used (LRU)
• Replace that page which CPU has not used since longest time.
• Assume:
1. No. of frames = 3 (all empty initially)
2. Page reference sequence = 1,2,3,4,1,2,5,1,2,3,4,5
3. Find out the number of page fault ?
Least Recently Used (LRU)
Advantages
• Efficient for practical usage
• Doesn’t suffer from Belady’s Anomaly

Disadvantages
• Complex implementation
• Expensive
Question
• No. of frames = 5 (all empty initially)
• Page reference sequence = 3,8,2,3,9,1,6,3,8,9,3,6,2,1,3
• Find out the page faults using LRU policies ?

You might also like