Tutorial 7, PPT - Dr. Aiman Hanna

advertisement
Tutorial 7
Memory Management
presented by:
Antonio Maiorano
Paul Di Marco
Types of Memory
• Primary Memory (a.k.a. RAM)
– Holds data and programs used by a
process that is executing
– Only type of memory that a CPU deals with
• Secondary Memory (i.e. hard disk)
– Non-volatile memory used to store data
when a process is not executing
The Memory Manager (MM)
• Purpose: to manage the use of primary
and secondary memory.
• Responsible for:
– Allocating primary memory to processes
– Moving processes between primary and
secondary memory
– Optimizing memory usage
Within a Process
Process Memory
There are two types of memory that can
be used within a process:
• Stack: used for local/automatic
variables and for passing parameters to
function calls
• Heap: used for dynamic memory
allocation
Process Memory Layout
Text Segment
Data Segment (global and
static variables)
Heap Storage
Stack Segment
Environment Variables, etc.
• Allocates more memory
than needed at first
• Heap grows towards
stack for dynamic
memory allocation
• Stack grows towards
heap when automatic
variables are created
Between Processes
Memory Allocation
• When a process is ready to run, it
requests memory from the MM
• Different strategies can be used to
allocate memory:
– Fixed-partition strategies
– Variable-partition strategies
Fixed-Partition Strategies
• Memory divided into fixed-size regions
• Size of each region usually not equal
• MM will allocate a region to a process
that best fits it
• Unused memory within an allocated
partition called internal fragmentation
An Example
Partition 1
Partition 2
Operating
System
Process 1
Internal Fragmentation
Partition 3
Partition 4
Partition 5
Partition 6
Partition 7
Process 2
Process 3
Internal Fragmentation
Problems…
• Not suitable for systems in which
process memory requirements not
known ahead of time; i.e. timesharing
systems
• Sometimes, user is idle; other times,
can be running many programs –
memory requirements change!
Variable-Partition Strategies
• MM allocates regions equal to the
memory requirements of a process at
any given time
• As processes die, holes develop in the
memory
• MM inserts new processes into holes
using a best fit strategy
More…
• Results in External Fragmentation
• After a while, only small processes will
be able to run due to too much external
fragmentation
• MM must compact the memory to make
more space available for larger
processes
An Example
Operating
System
Operating
System
Operating
System
Process 1
Process 5
Process 5
Process 4
Process 2
Process 3
Initially
Process 4
Process 3
Process 3
P2 completes
P4 starts
P1 completes
P5 starts
After compacting
Memory Management
Strategies
MM Strategies
• Since most OSs offer multiprogramming,
we study multiple-partition MM strategies
• We want to maximize the number of
(active) processes that can be “in
memory” at the same time
• Makes use of “secondary storage”
Swapping
• Comes from the basis that when a
process is blocked, it does not need to
be in memory
• Thus, it is possible to save a process’
entire address space to disk
• Implemented on Windows 9X/Me,
saving to a “swap file”
Pros/Cons
• Simple strategy that does not need
much extra hardware (process
relocation)
• Inefficient to swap large address spaces
for short times!
Virtual Memory
• Comes from the basis that all of a
process’ address space is not needed
at once
• Thus, chop up the address space into
smaller parts and only load the parts
that are needed
• These parts need not be contiguous in
memory!
VM Usage
• Virtual memory is used in most modern
operating systems:
– Windows NT/2000/XP uses one or more
“page files” to swap pages
– Linux uses a hard disk partition (“swap
partition”) to swap to
Pros/Cons
• Since only the necessary parts of the process
are loaded, processes load faster and it
allows much better memory utilization
• Needs lots of extra hardware to accomplish
the job (efficiently)
• In some cases too much paging (i.e.
“thrashing”) can occur, which is very slow
Download