Sei sulla pagina 1di 27

Memory Management

Lecture

Sadaqat Ali Khan Bangash


Institute of Information Technology,
University of Science & Technology Bannu

Memory Management Sadaqat Ali Khan Bangash 1


Memory Hierarchies

• Like everything else in computers, Computer Memory is also


organized into a hierarchy. This is as follows:

• At 1st Level (closest to Processor) are the registers


• At 2nd Level, one or two Levels of Cache Memory
• At 3rd Level, Main Memory (also known as primary memory)

Memory Management Sadaqat Ali Khan Bangash 2


Memory Hierarchies ….

• All above are considered part of Internal


Memory (i.e. Internal to Computer System).
Then the Hierarchy continues onto External
Memory

• At 4th Level, Secondary Storages (Hard disks,


tapes etc)
• At 5th Level, other forms of memory comes
such as Zip Disks, Floppy Drives, etc

Memory Management Sadaqat Ali Khan Bangash 3


Internal
Memory

Register,
Cache,
Main Memory

External
Hard disk. Memory
CD-Rom,DVD,
Internal Storage
Memory Management Sadaqat Ali Khan Bangash 4
Memory Hierarchies ….

• Registers
– These temporary locations within the CPU are
extremely fast, very small, very expensive & volatile.


• Cache
– Buffer memory regions between CPU & Main
Memory.

Memory Management Sadaqat Ali Khan Bangash 5


Memory Hierarchies ….

Main Memory
– Main Memory also called Primary Memory, Random
Access Memory (RAM). Hundreds of Megabytes of
Medium Speed & Volatile
Secondary Memory
– Hundreds of Gigabytes of memory with Slow speed

We may recall the factors of Cost, Size, and Speed of


access that are the orders in which Memory
components are divided into this hierarchy.

Memory Management Sadaqat Ali Khan Bangash 6


Purpose of Memory Management

• To ensure fair use of memory for processes


• To ensure secure access, i.e., space allocated
for process is not allocated for another process,
or a process does not have access to anything
beyond its "process address space“
• To ensure orderly memory access, i.e., when
memory is to be allocated, when de-allocated,
etc. etc.
• To ensure efficient use of memory. Since
memory is expensive, it should be used
efficiently

Memory Management Sadaqat Ali Khan Bangash 7


Uni-programming environment

• In a Uni-programming environment, main


memory is divided into two parts:

1. One part for the Operating System

2. One part for the Program currently being


executed. (or the User Part)

Memory Management Sadaqat Ali Khan Bangash 8


Multi-programming environment

• In a multi-programming environment, the user part of the


memory is further divided to accommodate multiple
processes. This task of dividing and allocation is carried out
dynamically by the Operating System and is known as
memory management.
• Memory Manager has the following basic responsibilities:
1. Keep track of used & free memory spaces (Also known
as Free Space Management)
2. When, where and how much memory to allocate or de-
allocate
3. Swapping processes in and out of main memory to
secondary memory.

Memory Management Sadaqat Ali Khan Bangash 9


Logical & Physical Addresses

Logical Address
An address generated by the CPU
This normally refers to an instruction or data
within the process address space

Physical Address
An address for a main memory location where instruction
or data resides

Memory Management Sadaqat Ali Khan Bangash 10


Logical & Physical Addresses …

• Since the Process is already loaded in memory, the logical


addresses of the process need to be translated to its
corresponding physical addresses.
• The CPU generates all logical Addresses for a particular process.
A set of these logical addresses constitute the "Logical Address
Space". Correspondingly, all the physical addresses that
translate back to the logical addresses are known as the
"Physical Address Space" for that process.
• This will be explained in detail in paging & segmentation. The
logical address is also known as the Offset Address. So in simple
words ,

Memory Management Sadaqat Ali Khan Bangash 11


Logical & Physical Addresses …

Logical Address Space


The set of all logical addresses generated by a process

Physical Address Space


The set of all physical addresses corresponding to those
logical addresses

This translation or mapping from Logical to


Physical address is done by a piece of CPU
hardware known as the Memory Management
Unit.
Memory Management Sadaqat Ali Khan Bangash 12
MMU Process

Logical Physical
Address Address

CPU
346 14346

Base or
Relocation
Register Value:
14000

14000

The MMU will check if the logical address is within the limit
specified by the limit register. If yes, it will add the value of the
relocation register. If no, it will give an addressing error or a
trap.

Memory Management Sadaqat Ali Khan Bangash 13


Using Memory Efficiently
Dynamic Loading

• With dynamic loading, a routine is not loaded into main memory


until it is called. All these routines are kept on the disk in a re-
locatable format. The main program is loaded into memory &
executed.
• The best example for explaining this would be MS-Word. Word
had thousands of features. At any given time, a user will only be
using a couple of these features. The routines for those
additional features are kept on disk whereas only the main
program for MS-Word is loaded.
• Dynamic Loading is also used in handling exceptions or errors.
Routines for handling exceptions or errors are only loaded when
they occur, otherwise they will remain on disk.

Memory Management Sadaqat Ali Khan Bangash 14


Using Memory Efficiently …

• Advantages:
– Less time needed to load a program
– Less memory space needed for program

• Disadvantages:
– More run-time activity. Lot of I/O requests are made
which is time consuming.

Memory Management Sadaqat Ali Khan Bangash 15


Dynamic Linking …

• Advantages:
• Less time needed to load a program
• Less memory space needed
• Less disk space needed to store binaries
• Updated libraries are used without
recompiling a program
• Disadvantages:
• More run-time activity resulting in slower
program execution

Memory Management Sadaqat Ali Khan Bangash 16


Overlays
• Overlays allow a process to be larger than the
amount of memory allocated to it. It works by
keeping only those instructions in memory
that are needed at any given time. I.e., lets
suppose our Main Memory is 100 MB, will an
executable of 110 MB will be able to run? The
answer is yes if the underlying Operating
Systems employs Virtual Memory techniques.
This is the concept behind Virtual Memory.
• It is essential to understand Overlays because
it gives a flavor of Virtual Memory which we
would be cover later on.

Memory Management Sadaqat Ali Khan Bangash 17


Overlays…
• When other instructions & data are needed,
they are loaded into the space occupied
previously by instructions that are no longer
needed. Overlays are implemented solely by
the programmer. If the program does not
have support for Virtual Memory or Overlays,
then the Operating System cannot do anything
about it.

Memory Management Sadaqat Ali Khan Bangash 18


Swapping
• A process can be swapped temporarily out of memory to a
backing store, and then brought back into memory for
continued execution.

• Backing store – fast disk large enough to accommodate


copies of all memory images for all users; must provide
direct access to these memory images

• Roll out, roll in – swapping variant used for priority-based


scheduling algorithms; lower-priority process is swapped out
so higher-priority process can be loaded and executed

• Major part of swap time is transfer time; total transfer time is


directly proportional to the amount of memory swapped
Memory Management Sadaqat Ali Khan Bangash 19
Swapping

• Main Memory
• Secondary Memory
• The area where swapped processes are kept in secondary
memory is known as Swap Space

• Memory Management Techniques


– Multi-Programming with Fixed Number of Tasks (MFT)
– Multi-Programming with Variable Number of Tasks (MVT)
– Paging
– Segmentation
– Virtual Memory

Memory Management Sadaqat Ali Khan Bangash 20


Swapping
Modified versions of swapping are found on many systems (i.e.,
UNIX, Linux, and Windows)
System maintains a ready queue of ready-to-run processes which
have memory images on disk

Memory Management Sadaqat Ali Khan Bangash 21


Contiguous Allocation
• Main memory usually divided into two partitions:
– Resident operating system, usually held in low
memory with interrupt vector
– User processes then held in high memory

• Relocation registers used to protect user processes


from each other, and from changing operating-
system code and data
– Base register contains value of smallest physical
address
– Limit register contains range of logical addresses –
each logical address must be less than the limit register
– MMU maps logical address dynamically
Memory Management Sadaqat Ali Khan Bangash 22
HW address protection with base and limit registers

Memory Management Sadaqat Ali Khan Bangash 23


Contiguous Allocation (Cont.)
• Multiple-partition allocation
– Hole – block of available memory; holes of various size
are scattered throughout memory
– When a process arrives, it is allocated memory from a
hole large enough to accommodate it
– Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
OS OS OS OS

process 5 process 5 process 5 process 5


process 9 process 9

process 8 process 10

process 2 process 2 process 2 process 2

Memory Management Sadaqat Ali Khan Bangash 24


Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes

• First-fit: Allocate the first hole that is big enough


• Best-fit: Allocate the smallest hole that is big enough;
must search entire list, unless ordered by size
– Produces the smallest leftover hole
• Worst-fit: Allocate the largest hole; must also search
entire list
– Produces the largest leftover hole
First-fit and best-fit better than worst-fit in terms of speed and
storage utilization

Memory Management Sadaqat Ali Khan Bangash 25


Fragmentation
• External Fragmentation – total memory space exists
to satisfy a request, but it is not contiguous
• Internal Fragmentation – allocated memory may be
slightly larger than requested memory; this size
difference is memory internal to a partition, but not
being used
• Reduce external fragmentation by compaction
– Shuffle memory contents to place all free memory
together in one large block
– Compaction is possible only if relocation is
dynamic, and is done at execution time
– I/O problem
• Latch job in memory while it is involved in I/O
• Do I/O only into OS buffers
Memory Management Sadaqat Ali Khan Bangash 26
End of Lecture

Questions

Memory Management Sadaqat Ali Khan Bangash 27

Potrebbero piacerti anche