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

20
Tutorial 6 Memory Management Memory Management 1

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

Page 1: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

Tutorial 6Memory Management

Memory Management 1

Page 2: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 3: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 4: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

Memory Management 4

Page 5: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 6: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 7: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

Memory Management 7

Page 8: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 9: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 10: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 11: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 12: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 13: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 14: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 15: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

Memory Management 15

Page 16: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Page 17: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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.

Page 18: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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!

Page 19: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of 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.

Page 20: Tutorial 6 Memory Management 1. Types of Memory Primary Memory (RAM) Holds data and programs used by a process that is executing. Only type of memory.

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

Memory Management 20