Module 7 Memory Management
Module 7 Memory Management
As processes are loaded and removed from memory, the free memory space is broken into little pieces. It happens
after sometimes that processes cannot be allocated to memory blocks considering their small size and memory
blocks remains unused. This problem is known as Fragmentation.
2 Types of Fragmentation
a.) External fragmentation- the available memory is broken up into lots of little pieces, none of which is
big enough to satisfy the next memory requirement, although the sum total could.
b.) Internal fragmentation- memory block assigned to process is bigger. Some portion of memory is left
unused, as it cannot be used by another process
As you have learned in the previous lesson, memory management is one of the most important features of
the operating system because it affects the execution time of process directly. The execution time of the process
directly depends upon the availability of data in the main memory. Therefore, an operating system must perform its
memory management in such a form that the essential data is always present in the main memory.
The following are the different memory allocation strategies used by the operating system:
1.) Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of main memory (or
move) to secondary storage (disk) and make that memory available to other processes. At some later time, the
system swaps back the process from the secondary storage to main memory.
Figure 2. Swapping
Source:tutorialspoint.com
Though performance is usually affected by swapping process but it helps in running multiple and
big processes in parallel and that's the reason Swapping is also known as a technique for memory
compaction.
Most modern OSes no longer use swapping, because it is too slow and there are faster alternatives
available. However some UNIX systems will still invoke swapping if the system gets extremely full, and
then discontinue swapping when the load reduces again.
One approach to memory management is to load each process into a contiguous space. The operating
system is allocated space first, usually at either low or high memory locations, and then the remaining available
memory is allocated to processes as needed.
In contiguous memory allocation each process is contained in a single contiguous block of memory.
Memory is divided into several fixed size partitions. Each partition contains exactly one process. When a
partition is free, a process is selected from the input queue and loaded into it. The set of holes (available
memories) is searched to determine which hole is best to allocate.
The following are the three(3) most discussed contiguous memory allocation:
1. First Fit: The first hole that is big enough is allocated to program.
2. Best Fit: The smallest hole that is big enough is allocated to program.
3. Worst Fit: The largest hole that is big enough is allocated to program.
First and best fits experience fragmentation problems more so than worst fit.
3.) Paging
Paging is a memory management scheme where physical memory is divided into a number of
equal sized blocks called frames, and where programs’ logical memory space is divided into blocks of the
same size called pages. It is the predominant memory management technique used today.
There is no external fragmentation with paging because all blocks of physical memory are used,
and there are no gaps in between and no problems with finding the right sized hole for a particular chunk of
memory. However, internal fragmentation occurs.
Figure 3. Paging
Source:tutorialspoint.com
4.) Segmentation
Segmentation is a memory management technique in which each job is divided into several segments
of different sizes. Each segment is actually a different logical address space of the program. Segmentation
memory management works very similar to paging but here segments are of variable-length where as in paging
pages are of fixed size.
Figure 4. Segmentation
Source:tutorialspoint.com
A computer can address more memory more than the amount physically installed on the system. This extra
memory is actually called virtual memory and it is a section of a hard disk that's set up to emulate the computer's
RAM.
Virtual Memory stores large program in form of pages during their execution and only the required pages
or portions of processes are loaded into the main memory. This technique is useful as large virtual memory is
provided for user programs when a very small physical memory is there.
Modern microprocessors intended for general-purpose use, a memory management unit, or MMU, is built into
the hardware. The MMU's job is to translate virtual addresses into physical addresses. A basic example is given
below –
There are two major requirements to implement a successful demand paging system these are the frame-
allocation algorithm and page-replacement algorithm.
Page-replacement algorithm
This algorithm deals with how to select a page loaded for replacement when there are no free frames
available for a requested page that is not loaded in the memory.
Basic Page Replacement Algorithm
1. Find the location of the page requested by ongoing process on the disk.
2. Find a free frame. If there is a free frame, use it. If there is no free frame, use a page-replacement
algorithm to select any existing frame to be replaced, such frame is known as victim frame.
3. Write the victim frame to disk and change all related page tables to indicate that this page is no longer in
memory.
4. Move the required page and store it in the frame. Adjust all related page and frame tables to indicate the
change.
5. Restart the process that was waiting for this page.
Reference String
The string of memory references is called reference string. Reference strings are generated artificially or
by tracing a given system and recording the address of each memory reference.
-For a given page size, we need to consider only the page number, not the entire address.
-If we have a reference to a page p, then any immediately following references to page p will never cause a
page fault. Page p will be in memory after the first reference; the immediately following references will not
fault.
For example:
Consider the following sequence of addresses − 123,215,600,1234,76,96
-If page size is 100, then the reference string is 1,2,6,12,0,0
Frame-allocation Algorithm
These algorithm centers around how many frames (memory blocks) are allocated to each process (and to
other needs).
Equal Allocation
When there are available frames and processes to share them, the frames will be equally divided
equally and each process gets the same number of frames.
Proportional Allocation
Allocates the frames proportionally to the size of the process, relative to the total size of all
processes
Priority allocation
Higher priority processes get more frames.