#OS Lecture Note 3 Memory Management (2)
#OS Lecture Note 3 Memory Management (2)
Mattu University
Engineering and Technology College
Department of Computer Science
Operating System
By: Tekalign B.
Lecture Three
Memory Management
Swapping
Partitioning
Caching
2/29/2024
Memory management in OS
4
2/29/2024
What is Main Memory?
5
2/29/2024
The need for memory management
6
• Memory is cheap today, and getting cheaper
– But applications are demanding more and more memory, there is
never enough!
Ideally programmers want memory that is
• Fixed Partitioning
• Dynamic Partitioning
• Simple Paging
• Simple Segmentation
• Virtual Memory Paging
• Virtual Memory Segmentation
Fixed Partitioning
• Equal-size partitions
– Any process whose size is less than or
equal to the partition size can be loaded
into an available partition
• If all partitions are occupied, the operating
system can swap a process out of a partition
– If none are in a ready or running state
Fixed Partitioning: Problems
• A hole of 64K is left after loading 3 processes: not enough room for another
process
• Eventually each process is blocked. The OS swaps out process 2 to bring in
process 4=128K
Dynamic Partitioning example …
26
• First-fit algorithm
– Scans memory form the
beginning and chooses the first
available block that is large
enough
– Fastest
– May have many process loaded in
the front end of memory that must
be searched over when trying to
find a free block
Dynamic Partitioning
29
• Next-fit
– Scans memory from the location of the last
placement
– More often allocate a block of memory at the end of
memory where the largest block is found
– Inefficient memory utilization is a major issue in the
worst fit.
Dynamic Partitioning
30
• Worst-fit
– allocate the largest available hole
to process.
– This method produces the largest
leftover hole.
Fragmentation
31
Internal fragmentation:
occurs when memory blocks are allocated to the process more than
Due to this some unused space is left over and creating an internal
fragmentation problem.
Example: Suppose there is a fixed partitioning used for memory
allocation and the different sizes of blocks 3MB, 6MB, and 7MB
space in memory. Now a new process p4 of size 2MB comes and
demands a block of memory. It gets a memory block of 3MB but
1MB block of memory is a waste, and it can not be allocated to
other processes too. This is called internal fragmentation.
Fragmentation
33
External fragmentation:
we have a free memory block, but we can not assign it to a
process because blocks are not contiguous.
Example: Suppose (consider the above example) three
processes p1, p2, and p3 come with sizes 2MB, 4MB,
and 7MB respectively. Now they get memory blocks of
size 3MB, 6MB, and 7MB allocated respectively. After
allocating the process p1 process and the p2 process left
1MB and 2MB. Suppose a new process p4 comes and
demands a 3MB block of memory, which is available, but
we can not assign it because free memory space is not
contiguous. This is called external fragmentation.
Fragmentation
34
• Advantages of Paging
– Paging reduces external fragmentation,
– simple to implement and assumed as an efficient memory
management technique.
– Paging is useful for fast accessing data.
– Due to equal size of the pages and frames, swapping becomes
very easy.
• Disadvantages of Paging
– suffer from internal fragmentation.
– Page table requires extra memory space, so may not be good for
a system having small RAM.
Page Replacement Algorithm
44
Oldest page in main memory is the one which will be selected for
replacement.
The operating system maintains a list of all pages currently in
memory, with the most recent arrival at the tail and the least recent
arrival at the head. On a page fault, the page at the head is removed
and the new page added to the tail of the list. Oldest page in main
memory is the one which will be selected for replacement.
Easy to implement, keep a list, replace pages from the tail and add
new pages at the head.
Cont’d…
46
Cont’d…
47
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
The optimal page replacement algorithm says that the page with the
highest label should be removed. If one page will not be used for 8
million instructions and another page will not be used for 6 million
instructions, removing the former.
Replace the page that will not be used for the longest period of time
. Use the time when a page is to be used.
Cont’d…
49
Cont’d…
50
Least Recently Used (LRU) algorithm
51
Page which has not been used for the longest time in main memory
is the one which will be selected for replacement.
Types of Catching?
Cpu cache, Memory cache, Disk cache, Browser cache, Distributed cache
Answer
A memory leak occurs when a program fails to release
memory that it no longer needs, resulting in wasted
memory resources. Over time, if memory leaks accumulate,
the system’s available memory diminishes, leading to
reduced performance and possibly system crashes.
FAQ?
62
Dis adv
introduces additional overhead due to page table lookups and potential
performance degradation if excessive swapping occurs.
End of Chapter Three