Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

21
Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco

Transcript of Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

Page 1: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

Tutorial 7

Memory Management

presented by:

Antonio Maiorano

Paul Di Marco

Page 2: 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

Page 3: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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

Page 4: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

Within a Process

Page 5: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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

Page 6: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

Process Memory Layout

• Allocates more memory than needed at first

• Heap grows towards stack for dynamic memory allocation

• Stack grows towards heap when automatic variables are created

Text Segment

Data Segment (global and static variables)

Heap Storage

Stack Segment

Environment Variables, etc.

Page 7: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

Between Processes

Page 8: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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

Page 9: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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

Page 10: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

An Example

Operating System

Process 1

Process 2

Process 3

Partition 1

Partition 2

Partition 3

Partition 4

Partition 5

Partition 6

Partition 7

Internal Fragmentation

Internal Fragmentation

Page 11: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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!

Page 12: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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

Page 13: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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

Page 14: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

An Example

Operating System

Process 1

Process 2

Process 3

Operating System

Process 5

Process 3

Process 4

Operating System

Process 5

Process 3

Process 4

Initially P2 completesP4 startsP1 completesP5 starts

After compacting

Page 15: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

Memory Management Strategies

Page 16: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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”

Page 17: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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”

Page 18: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

Pros/Cons

• Simple strategy that does not need much extra hardware (process relocation)

• Inefficient to swap large address spaces for short times!

Page 19: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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!

Page 20: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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

Page 21: Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.

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