Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by...

Post on 26-Dec-2015

215 views 0 download

Transcript of Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by...

Tutorial 6Memory Management

Memory Management 1

Types of MemoryPrimary Memory (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.

Memory Management 2

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.Memory Management 3

Memory Management 4

Process MemoryThere are two types of memory that can be used within a process:

Stack: used for local variables and for passing parameters to function calls.

Heap: used for dynamic memory allocation.

Memory Management 5

Process Memory Layout

Memory Management 6

Text Segment

Data Segment (global and static variables)

Heap Storage

• Allocates more memory than needed at first.

• Heap grows towards stack for dynamic memory allocation.

• Stack grows towards heap when automatic variables are created.

Environment Variables, etc.

Stack Segment

Memory Management 7

Memory AllocationWhen a process is ready to run, it

requests memory from the MM.Different strategies can be used to

allocate memory:Fixed-partition strategiesVariable-partition strategies

Memory Management 8

Fixed-Partition StrategiesMemory is divided into fixed-size regions.

Size of each region usually is not equal.

MM will allocate a region to a process that best fits it.

Unused memory within an allocated partition is called internal fragmentation.

Memory Management 9

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

Memory Management 10

Problems …Not suitable for systems in which process memory requirements is not known ahead of time; i.e., timesharing systems.

Sometimes, user is idle; other times, memory requirements change!

Memory Management 11

Variable-Partition StrategiesMM 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.

Memory Management 12

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.

Memory Management 13

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

Memory Management 14

Memory Management 15

MM StrategiesSince 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”.

SwappingComes 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.

Virtual MemoryComes 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!

Virtual Memory UsageVirtual memory is used in most

modern operating systems:Windows NT/XP/ X uses one or more “page files” to swap pages.

Linux uses a hard disk partition (“swap partition”) to swap to.

Virtual MemoryMore details about Virtual Memory will be in the next tutorial.

Memory Management 20