0% found this document useful (0 votes)
26 views46 pages

Memory Management

Memory management is a critical function of an operating system that oversees the allocation and tracking of primary memory, ensuring efficient process execution and protection between processes. It involves address binding, memory protection, and various storage allocation techniques, including contiguous and non-contiguous methods like paging and segmentation. Additionally, concepts such as virtual memory and demand paging are employed to optimize memory usage and manage inactive programs effectively.

Uploaded by

shagun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views46 pages

Memory Management

Memory management is a critical function of an operating system that oversees the allocation and tracking of primary memory, ensuring efficient process execution and protection between processes. It involves address binding, memory protection, and various storage allocation techniques, including contiguous and non-contiguous methods like paging and segmentation. Additionally, concepts such as virtual memory and demand paging are employed to optimize memory usage and manage inactive programs effectively.

Uploaded by

shagun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Memory Management

Memory Management
• Memory management is the functionality of an operating
system which handles or manages primary memory and
moves processes back and forth between main memory and
disk during execution.

• Memory management keeps track of each and every


memory location, regardless of either it is allocated to some
process or it is free.

• It checks how much memory is to be allocated to processes.

• It decides which process will get memory at what time.

• It tracks whenever some memory gets freed or unallocated


and correspondingly it updates the status.
Why Memory management
1. ? To ensure protection of different processes from
each other (so that they do not interfere with
each other’s operations).

2. To place the programs in memory (such that


memory is utilized to its fullest extent).
Address binding
Address binding is the process of mapping from one address
space to another address space. Logical address is address
generated by CPU(address of an instruction or data) during
execution whereas Physical Address refers to location in
memory unit(the one that is loaded into memory)
An address binding can be done in three different
ways:
Compile Time – If you know that during compile time where process
will reside in memory then absolute address is generated i.e physical
address is embedded to the executable of the program during
compilation. Loading the executable as a process in memory is very
fast.

Load time – If it is not known at the compile time where process will
reside then relocatable address will be generated. Loader translates
the relocatable address to absolute address. The base address of the
process in main memory is added to all logical addresses by the loader
to generate absolute address. In this if the base address of the
process changes then we need to reload the process again.
Execution time/Run time- The instructions are in memory and
are being processed by the CPU. Additional memory may be
allocated and/or deallocated at this time. This is used if
process can be moved from one memory to another during
execution(dynamic linking-Linking that is done during load or
run time). e.g – Compaction.

MMU(Memory Management Unit)-


The run time mapping between Virtual address and Physical
Address is done by hardware device known as MMU.
Logical vs. physical address

space
A logical address is the address of an
instruction or data as used by a program.

►A physical address is the effective memory


address of an instruction or data i.e. it is the
address obtained after binding of logical
addresses has been done.
Relocation register
► Relocation register is a special register in the
CPU used for program relocation means mapping
of logical addresses used by the program to
physical addresses of the system’s main memory
Static vs Dynamic Loading
Loading the entire program into the main memory
before start of the program execution is called
as static loading.

Loading the program into the main memory on


demand is called as dynamic loading.
Memory Protection
Memory protection is a phenomenon by which we
control memory access rights on a computer.

The main aim of it is to prevent a process from


accessing memory that has not been allocated to it.

Hence prevents a bug within a process from affecting


other processes, or the operating system itself, and
instead results in a segmentation fault or storage
violation exception being sent to the disturbing process,
generally killing of process.
Memory Management Techniques
Storage Allocation and
Management
Techniques
The Storage allocation can be of two types:

(i) Contiguous storage allocation.

(ii) Non-contiguous storage allocation.


Contiguous Storage Allocation
► 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.

► It is further subdivided into Fixed-partition storage


allocation strategy and variable- partition storage
allocation strategy.
1. Single Contiguous memory Allocation
or fixed sized partition(Static
partition)
► Processes with small address space use small
partitions and processes with large address space
use large partitions. This is known as fixed partition
contiguous storage allocation.

Advantage: Management or accounting is simple.

Disadvantage: Occurrence of Internal fragmentation.


2. Variable-partition contiguous storage
allocation (Dynamic Partition)

Whenever a process needs memory, a search for the


space needed by it, is done.
If contiguous space is available to accommodate that
process, then the process is loaded into memory.

Advantage: This technique is free from internal fragmentation.

Disadvantage: Management is troublesome as memory is


getting absolutely divided after some time.
External Fragmentation
► This phenomenon of entering and leaving the
memory can cause the formation of unusable
memory holes (like the unused space between two
vehicles).This is known as External Fragmentation.
Three strategies that can be used to
allocate memory to Variable - partition
They are the following:

1. Best - Fit
2. Worst -
fit 3. First
- fit
Best - Fit - chooses a partition that is
smallest and whose size is greater than equal to k.
It leaves small-sized unusable partitions or
holes.
First - fit - chooses the first
partition whose size is greater than
equal to k.
Worst - fit - chooses the largest
partition and allocates it to process p. It
can leave bigger unusable partitions.
Compaction means to move the processes in the
memory in such a way that scattered pieces of
unused (free) memory can be placed together so
that any other process demanding contiguous
memory for it can use it.
Non-contiguous Storage
► To resolve Allocation
the problem of external fragmentation
and to enhance the degree of multiprogramming
to a greater extent, it was decided to sacrifice the
simplicity of allocating contiguous memory to every
process.
► It was decided to have a non-contiguous physical
address space of a process so that a process could be
allocated memory wherever it was available.
Non- contiguous Storage Allocation
There are 2 techniques for non- contiguous allocation:
1. Paging
2. Segmentation
1. Paging
• In this approach, physical memory is divided into fixed-size
blocks called frames and the logical memory is divided
into the fixed-sized blocks called pages.

• The size of a page is same as that of frame.

• The key idea of this method is to place the pages of a


process into the available frames of memory, whenever,
this process is to be executed.

• The address mapping is done by Page table.


Paging example
PAGE NO.
FRAME NO.
It works as:
Translate logical address 13 = 01101 in
binary
Offset = 01 in binary or 1 in decimal Page
no. = 011 in binary or 3 in decimal
Physical address = pageframe*pagesize +
offset i.e. 2*4+1 = 9 (which is the real
address of page containing character “n” in
the main memory)
Problem with
paging
► It uses a page table per process for
translating logical to physical address
space. A basic issue associated with
the use of page table is, that the look up
in the page table should be very fast
(which is generally slow), as it is done on
every memory reference.
► So to make the look up time less, caching
is used. In this case, the specific cache
device is called a translation look aside
buffer (TLB).
Translation look aside buffer (TLB).
► The TLB contains a set of entries, each of
which contains a page number, the
corresponding frame number, and the
protection bits.
► So if we use TLB, then mapping is done
by TLB and we generally find the entry in
TLB but in case the entry is not found in
TLB than we have a TLB miss and finally
we go to page table.
Segmentation
► segmentation is another technique for the
noncontiguous storage allocation. It is
different from paging as it supports users’ view
of his program.
► For a programmer it might be more relevant to
divide the logical address space of his program
into variable sized segments (with respect to his
view of main program, subroutines, data, etc.)
than to divide it into fixed size pages. Such
variable sized segments, which are a collection
of logically related information, are the basis of
segmentation technique.
Segmentation example
Virtual
Memory
► Virtual memory is a technique of executing
program instructions that may not fit entirely in
system memory. This is done by calling
instructions as and when the application requires
it. Virtual memory is implemented by using
secondary storage to augment the main
memory. Data is transferred from secondary to
main storage as and when necessary and the
data modified is written back to the secondary
storage according to a predetermined algorithm.
Swappin
g technique of temporarily
► Swapping is the
removing inactive programs from the main
memory of a computer system. An
inactive program is one that is neither
executing nor performing an I/O operation.
► This may also happen when it is desired to
place a higher-priority process in the
memory. A lower priority process may
be swapped out so that higher-priority
process may be loaded and executed.
Demand
Paging
► In virtual memory system, demand paging is a
type of swapping in which pages of programs
are not copied from disk to main memory
until they are needed for execution.
► In demand paging, virtual address (logical
address) is accessed by CPU, the
corresponding page number is looked up in the
page table and if it shows that currently this
page is not in main memory, then this page must
be brought into the main-memory.
► A page fault occurs when an invalid
page is addressed. Page fault must be
followed by swapping-in the page
(demanded just now by the CPU) from
disk to main-memory or a trap should be
generated to the operating system if the
page being demanded is not within the
logical address space of the process.
To determine whether the reference to
the requested page is within the logical
address space or not, an internal table
may be consulted
Pure Demand
► PurePaging
demand paging is the form of
demand paging in which not even
a
single page is loaded into
memory, initially. Thus, the
very first instruction
causes a page fault in this case. This
kind of demand paging may significantly
decrease the performance of a computer
system by generally increasing the
effective access time of memory.
Page
Replacement
► Once the main memory fills up, a page
must be swapped out to make room for
any pages to be swapped in. This is
known as page replacement.
► We have page replacement algorithms for
page replacement purpose, which are as
follows:
1. The optimal page
replacement algorithm
2. The first-in, first-out (FIFO)
page replacement
algorithm
3. The second chance page
replacement algorithm
4. The least recently used (LRU)
page replacement algorithm
5. Simulating LRU in Software (Not
frequently used algorithm)
6. The Working Set Page
Replacement Algorithm

You might also like